test(deno): serveDeno HTTP + WebSocket integration
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+150
-4
No files matched your search
@@ -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"] }
|
||||
}
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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",
|
||||
|
||||
@@ -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<string>((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();
|
||||
}
|
||||
});
|
||||
+2
-1
@@ -14,5 +14,6 @@
|
||||
"emitDecoratorMetadata": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"include": ["src", "test", "example"]
|
||||
"include": ["src", "test", "example"],
|
||||
"exclude": ["test/deno"]
|
||||
}
|
||||
+9
-2
@@ -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/**'],
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user