Files
bunext/src/__tests__/functions/server/handle-hmr.test.ts
T
2026-10-03 03:18:03 +01:00

43 lines
1.5 KiB
TypeScript

import { describe, expect, test, mock, beforeEach, afterEach } from "bun:test";
import handleHmr from "../../../../src/functions/server/handle-hmr";
describe("handle-hmr", () => {
beforeEach(() => {
global.BUNEXT_ROUTER = {
match: (path: string) => {
if (path === "/test") return { filePath: "/test-file" };
return null;
}
} as any;
global.BUNEXT_HMR_CONTROLLERS = [];
global.BUNEXT_BUNDLER_CTX_MAP = {
"test-file": { local_path: "/test-file" } as any,
};
});
afterEach(() => {
global.BUNEXT_ROUTER = undefined as any;
global.BUNEXT_HMR_CONTROLLERS = [];
global.BUNEXT_BUNDLER_CTX_MAP = undefined as any;
});
test("sets up SSE stream and pushes to BUNEXT_HMR_CONTROLLERS", async () => {
const req = new Request("http://localhost/hmr", {
headers: {
"referer": "http://localhost/test"
}
});
const res = await handleHmr({ req });
expect(res.status).toBe(200);
expect(res.headers.get("Content-Type")).toBe("text/event-stream");
expect(res.headers.get("Connection")).toBe("keep-alive");
expect(global.BUNEXT_HMR_CONTROLLERS.length).toBe(1);
const controller = global.BUNEXT_HMR_CONTROLLERS[0];
expect(controller.page_url).toBe("http://localhost/test");
expect(controller.target_map?.local_path).toBe("/test-file");
});
});