test(bun): serveBun HTTP + WebSocket integration

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-09 21:51:33 -05:00
1 parent fd6446f514
commit 4351b41038
3 files changed
+51 -2

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
// Real Bun integration test for serveBun: boots an app under the actual Bun runtime
// and round-trips both HTTP (app.fetch) and WebSocket (Bun's server-level websocket
// handler -> app.upgrade).
//
// Run with: npm run test:bun (or: bun test test/bun/)
import 'reflect-metadata';
import { test, expect } from 'bun:test';
import { createApp, Route, Get, Ws, ctx, Module } from '../../src/index.ts';
import { serveBun } from '../../src/bun.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 {}
test('serveBun: HTTP + WebSocket round-trip', async () => {
const app = createApp({ modules: [M] });
const server = serveBun(app, { port: 0 });
const port = server.port;
try {
const httpRes = await fetch(`http://127.0.0.1:${port}/rt/hello`);
expect(httpRes.status).toBe(200);
expect((await httpRes.json()).ok).toBe(true);
const ws = new WebSocket(`ws://127.0.0.1:${port}/rt/echo`);
const got = await new Promise<string>((resolve, reject) => {
ws.onopen = () => ws.send('hi');
ws.onmessage = (e) => {
resolve(String(e.data));
ws.close();
};
ws.onerror = () => reject(new Error('ws error'));
});
expect(got).toBe('echo:hi');
} finally {
server.stop(true);
}
});
+1 -1
View File
@@ -15,5 +15,5 @@
"forceConsistentCasingInFileNames": true
},
"include": ["src", "test", "example"],
"exclude": ["test/deno"]
"exclude": ["test/deno", "test/bun"]
}
+1 -1
View File
@@ -7,6 +7,6 @@ export default defineConfig({
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/**'],
exclude: [...configDefaults.exclude, 'test/deno/**', 'test/bun/**'],
},
});