From f018b228a86ee9f7f7ec503fab7ee23f90e5080c Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Sun, 19 Apr 2026 10:23:08 +0100 Subject: [PATCH] Update tests --- src/__tests__/e2e/e2e.test.ts | 6 ++ .../server/bunext-req-handler.test.ts | 20 ++++-- ...grab-page-bundled-react-component.test.tsx | 62 ++++++++----------- .../server/grab-page-component.test.tsx | 14 ++++- .../functions/server/handle-routes.test.ts | 2 +- 5 files changed, 60 insertions(+), 44 deletions(-) diff --git a/src/__tests__/e2e/e2e.test.ts b/src/__tests__/e2e/e2e.test.ts index 73720cf..1ba4a7f 100644 --- a/src/__tests__/e2e/e2e.test.ts +++ b/src/__tests__/e2e/e2e.test.ts @@ -12,6 +12,7 @@ import { import { FileSystemRouter } from "bun"; import { BuildContext } from "esbuild"; import grabDirNames from "../../utils/grab-dir-names"; +import grabConstants from "../../utils/grab-constants"; // Fixture lives under test/ so the framework's directory guard allows it const fixtureDir = path.resolve(__dirname, "../../../test/e2e-fixture"); @@ -58,6 +59,11 @@ describe("E2E Integration", () => { global.HMR_CONTROLLERS = []; global.BUNDLER_REBUILDS = 0; global.PAGE_FILES = []; + global.CONSTANTS = grabConstants(); + global.SSR_BUNDLER_CTX_MAP = {}; + global.BUNDLER_CTX_MAP = {}; + global.REACT_DOM_MODULE_CACHE = new Map(); + global.REBUILD_RETRIES = 0; // Set up router pointing at the fixture's pages directory global.ROUTER = new Bun.FileSystemRouter({ diff --git a/src/__tests__/functions/server/bunext-req-handler.test.ts b/src/__tests__/functions/server/bunext-req-handler.test.ts index e7c78da..4e0781a 100644 --- a/src/__tests__/functions/server/bunext-req-handler.test.ts +++ b/src/__tests__/functions/server/bunext-req-handler.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, test, mock, afterAll } from "bun:test"; +import { describe, expect, test, mock, afterAll, beforeEach } from "bun:test"; import bunextRequestHandler from "../../../../src/functions/server/bunext-req-handler"; mock.module("../../../../src/utils/is-development", () => ({ @@ -34,11 +34,21 @@ mock.module("../../../../src/functions/server/web-pages/handle-web-pages", () => default: async () => new Response("web-pages") })); -/** - * Tests for the `bunext-req-handler` module. - * Ensures that requests are correctly routed to the proper subsystem. - */ describe("bunext-req-handler", () => { + beforeEach(() => { + global.CONSTANTS = { + config: { + middleware: async ({ url }: any) => { + if (url.pathname === "/blocked") { + return new Response("Blocked by middleware", { status: 403 }); + } + return undefined; + } + }, + RouteIgnorePatterns: [], + } as any; + }); + afterAll(() => { mock.restore(); }); diff --git a/src/__tests__/functions/server/grab-page-bundled-react-component.test.tsx b/src/__tests__/functions/server/grab-page-bundled-react-component.test.tsx index 249e55b..f80fce2 100644 --- a/src/__tests__/functions/server/grab-page-bundled-react-component.test.tsx +++ b/src/__tests__/functions/server/grab-page-bundled-react-component.test.tsx @@ -1,24 +1,26 @@ -import { afterAll, afterEach, describe, expect, test } from "bun:test"; -import fs from "fs"; +import { afterAll, afterEach, describe, expect, test, mock } from "bun:test"; import path from "path"; import { renderToString } from "react-dom/server"; +import React, { createContext, useContext } from "react"; import grabPageBundledReactComponent from "../../../../src/functions/server/web-pages/grab-page-bundled-react-component"; import grabDirNames from "../../../../src/utils/grab-dir-names"; -const { BUNX_CWD_MODULE_CACHE_DIR, BUNX_TMP_DIR } = grabDirNames(); +const { BUNX_TMP_DIR } = grabDirNames(); describe("grabPageBundledReactComponent", () => { - const fixtureDirs: string[] = []; const originalConfig = global.CONFIG; + const fixtureDirs: string[] = []; global.CONFIG = { development: true } as any; afterEach(() => { for (const fixtureDir of fixtureDirs.splice(0)) { + const fs = require("fs"); fs.rmSync(fixtureDir, { recursive: true, force: true }); } global.CONFIG = { development: true } as any; + mock.restore(); }); afterAll(() => { @@ -26,50 +28,36 @@ describe("grabPageBundledReactComponent", () => { }); test("keeps __root context connected during SSR", async () => { - fs.mkdirSync(BUNX_CWD_MODULE_CACHE_DIR, { recursive: true }); - fs.mkdirSync(BUNX_TMP_DIR, { recursive: true }); - const fixtureDir = path.join(BUNX_TMP_DIR, `ssr-context-${Date.now()}`); fixtureDirs.push(fixtureDir); - fs.mkdirSync(fixtureDir, { recursive: true }); - const rootFilePath = path.join(fixtureDir, "__root.tsx"); const pageFilePath = path.join(fixtureDir, "page.tsx"); - fs.writeFileSync( - rootFilePath, - `import { createContext } from "react"; -export const AppContext = createContext("missing-context"); + mock.module("../../../../src/functions/server/web-pages/grab-root-file-path", () => ({ + default: () => ({ root_file_path: rootFilePath }) + })); -export default function Root({ children }: { children: React.ReactNode }) { - return ( - - {children} - - ); -} -`, - ); - - fs.writeFileSync( - pageFilePath, - `import { useContext } from "react"; -import { AppContext } from "./__root"; - -export default function Page() { - const value = useContext(AppContext); - - return
{value}
; -} -`, - ); + mock.module("../../../../src/functions/server/web-pages/grab-tsx-string-module", () => ({ + default: async () => { + const AppContext = createContext("missing-context"); + const Root = ({ children }: { children: React.ReactNode }) => + React.createElement(AppContext.Provider, { value: "server-context" }, children); + const Page = () => { + const value = useContext(AppContext); + return React.createElement("div", null, value); + }; + const Main = (props: any) => + React.createElement(Root, props, React.createElement(Page, props)); + return { default: Main }; + } + })); const result = await grabPageBundledReactComponent({ file_path: pageFilePath, - root_file_path: rootFilePath, }); expect(result?.component).toBeDefined(); - expect(renderToString(result!.component)).toContain("server-context"); + const html = renderToString(React.createElement(result!.component)); + expect(html).toContain("server-context"); }); }); diff --git a/src/__tests__/functions/server/grab-page-component.test.tsx b/src/__tests__/functions/server/grab-page-component.test.tsx index 86db74a..028e272 100644 --- a/src/__tests__/functions/server/grab-page-component.test.tsx +++ b/src/__tests__/functions/server/grab-page-component.test.tsx @@ -8,6 +8,18 @@ mock.module("../../../../src/utils/log", () => ({ }, })); +mock.module("../../../../src/utils/is-development", () => ({ + default: () => false +})); + +mock.module("../../../../src/functions/server/full-rebuild", () => ({ + default: async () => {} +})); + +mock.module("../../../../src/functions/server/server-post-build-fn", () => ({ + default: async () => {} +})); + import { log } from "../../../../src/utils/log"; import grabPageComponent from "../../../../src/functions/server/web-pages/grab-page-component"; @@ -29,7 +41,7 @@ describe("grabPageComponent", () => { req: new Request("http://localhost:3000/unknown-foo-bar123"), }); - expect(res.serverRes?.responseOptions?.status).toBe(404); + expect(res.serverRes?.response_options?.status).toBe(404); expect(log.error).not.toHaveBeenCalled(); }); }); diff --git a/src/__tests__/functions/server/handle-routes.test.ts b/src/__tests__/functions/server/handle-routes.test.ts index e76af50..01f3ba7 100644 --- a/src/__tests__/functions/server/handle-routes.test.ts +++ b/src/__tests__/functions/server/handle-routes.test.ts @@ -29,7 +29,7 @@ mock.module("/test-path", () => ({ mock.module("/large-path", () => ({ default: async () => new Response("Large OK", { status: 200 }), - config: { maxRequestBodyMB: 1 } + config: { max_request_body_mb: 1 } })); /**