From f85b39c2c3bf0408853da417c4d03300881d6eed Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Thu, 9 Jul 2026 17:23:24 -0500 Subject: [PATCH] test(deno): serveDeno HTTP + WebSocket integration Signed-off-by: Diego Resendez --- deno.json | 16 ++++++++++ deno.lock | 51 +++++++++++++++++++++++++++++++ package.json | 2 +- test/deno/ws.test.ts | 71 ++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 +- vitest.config.ts | 11 +++++-- 6 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 deno.json create mode 100644 deno.lock create mode 100644 test/deno/ws.test.ts diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..3a2fd5b --- /dev/null +++ b/deno.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "lib": ["deno.window", "deno.ns"] + }, + "unstable": ["sloppy-imports"], + "imports": { + "http": "node:http", + "https": "node:https", + "crypto": "node:crypto", + "events": "node:events", + "stream": "node:stream" + }, + "lint": { "exclude": ["dist", "node_modules"] } +} diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..c087fc3 --- /dev/null +++ b/deno.lock @@ -0,0 +1,51 @@ +{ + "version": "5", + "specifiers": { + "jsr:@std/assert@*": "1.0.19", + "jsr:@std/internal@^1.0.12": "1.0.14" + }, + "jsr": { + "@std/assert@1.0.19": { + "integrity": "eaada96ee120cb980bc47e040f82814d786fe8162ecc53c91d8df60b8755991e", + "dependencies": [ + "jsr:@std/internal" + ] + }, + "@std/internal@1.0.14": { + "integrity": "291516b3d4c35024d6ffbc0a9df5bf4c64116e05b50012cf846710152d2ffdf7" + } + }, + "workspace": { + "packageJson": { + "dependencies": [ + "npm:@eslint/js@^9.39.4", + "npm:@nestjs/common@^11.1.27", + "npm:@nestjs/core@^11.1.27", + "npm:@nestjs/platform-express@^11.1.27", + "npm:@nestjs/platform-fastify@^11.1.27", + "npm:@stylistic/eslint-plugin@^5.10.0", + "npm:@types/autocannon@^7.12.7", + "npm:@types/busboy@^1.5.4", + "npm:@types/express@^5.0.6", + "npm:@types/node@^18.19.0", + "npm:@types/ws@^8.18.1", + "npm:autocannon@8", + "npm:busboy@^1.6.0", + "npm:cognitive-complexity-ts@~0.8.2", + "npm:eslint@^9.39.4", + "npm:express@^5.2.1", + "npm:fastify@^5.10.0", + "npm:graphql@^16.9.0", + "npm:prettier@^3.9.4", + "npm:reflect-metadata@0.2.2", + "npm:rxjs@^7.8.2", + "npm:tsup@^8.5.1", + "npm:tsx@^4.23.0", + "npm:typescript-eslint@^8.63.0", + "npm:typescript@^5.5.0", + "npm:vitest@2", + "npm:ws@^8.21.0" + ] + } + } +} diff --git a/package.json b/package.json index 9a3d9e9..68ffafa 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "build": "tsup", "test": "vitest run", "test:dev": "vitest", - "test:deno": "deno test --allow-net --allow-read test/deno/", + "test:deno": "deno test --allow-net --allow-read --no-check test/deno/", "typecheck": "tsc --noEmit -p tsconfig.json", "typecheck:bench": "tsc --noEmit -p bench/tsconfig.json", "lint": "eslint src", diff --git a/test/deno/ws.test.ts b/test/deno/ws.test.ts new file mode 100644 index 0000000..4bd39e4 --- /dev/null +++ b/test/deno/ws.test.ts @@ -0,0 +1,71 @@ +// Real Deno integration test for serveDeno: boots an app under the actual Deno runtime +// and round-trips both HTTP (app.fetch) and WebSocket (Deno.upgradeWebSocket -> app.upgrade). +// +// Run with: npm run test:deno (or: deno test --allow-net --allow-read --no-check test/deno/) +// +// `--no-check`: the barrel (`src/index.ts`) re-exports types from Node-only modules +// (e.g. `src/http/*`) that reference ambient Node globals like `Buffer`. Those modules +// aren't executed under Deno (this test only exercises `serveDeno`'s fetch/WebSocket +// path), but Deno's checker still type-checks the whole reachable graph and has no +// ambient `Buffer` type without a full `@types/node` setup. Node-side static typing for +// those modules is already covered by `npm run typecheck` (tsc); this test's job is to +// prove Deno *runtime* behavior of the adapter, not re-typecheck Node-only internals. +import 'npm:reflect-metadata'; +import { assertEquals } from 'jsr:@std/assert'; +import { createApp, Route, Ws, Get, Module, ctx } from '../../src/index.ts'; +import { serveDeno } from '../../src/deno.ts'; + +@Route('/rt') +class Rt { + @Get('/hello') + hello() { + return { ok: true }; + } + + @Ws('/echo') + echo(@ctx() { inbound }: any) { + return (async function* () { + for await (const msg of inbound) yield `echo:${msg}`; + })(); + } +} + +@Module({ mountpoint: '/', controllers: [Rt] }) +class M {} + +Deno.test('serveDeno: HTTP + WebSocket round-trip through the graph', async () => { + const app = createApp({ modules: [M] }); + + let port = 0; + const server = serveDeno(app, { + port: 0, + onListen: (addr) => { + port = addr.port; + }, + }); + + // Let onListen fire before we try to connect. + await new Promise((resolve) => setTimeout(resolve, 50)); + + try { + const httpRes = await fetch(`http://127.0.0.1:${port}/rt/hello`); + assertEquals(httpRes.status, 200); + assertEquals((await httpRes.json()).ok, true); + + const ws = new WebSocket(`ws://127.0.0.1:${port}/rt/echo`); + try { + const got = await new Promise((resolve, reject) => { + ws.onopen = () => ws.send('hi'); + ws.onmessage = (e) => resolve(String(e.data)); + ws.onerror = () => reject(new Error('ws error')); + }); + assertEquals(got, 'echo:hi'); + } finally { + ws.close(); + // Give the close frame a moment to complete the handshake so no socket is leaked. + await new Promise((resolve) => setTimeout(resolve, 20)); + } + } finally { + await server.shutdown(); + } +}); diff --git a/tsconfig.json b/tsconfig.json index bea4487..5383782 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,5 +14,6 @@ "emitDecoratorMetadata": true, "forceConsistentCasingInFileNames": true }, - "include": ["src", "test", "example"] + "include": ["src", "test", "example"], + "exclude": ["test/deno"] } diff --git a/vitest.config.ts b/vitest.config.ts index f8fcf14..2343b4a 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,5 +1,12 @@ -import { defineConfig } from 'vitest/config'; +import { defineConfig, configDefaults } from 'vitest/config'; export default defineConfig({ - test: { globals: false, environment: 'node', include: ['test/**/*.test.ts'] }, + test: { + globals: false, + environment: 'node', + include: ['test/**/*.test.ts'], + // test/deno/** holds Deno.test files run separately via `npm run test:deno`; + // they use `npm:`/`jsr:` specifiers vitest can't resolve. + exclude: [...configDefaults.exclude, 'test/deno/**'], + }, });