Update tests

This commit is contained in:
Benjamin Toby 2026-04-19 10:23:08 +01:00
parent 95fcee36b2
commit f018b228a8
5 changed files with 60 additions and 44 deletions

View File

@ -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<string, any>();
global.REBUILD_RETRIES = 0;
// Set up router pointing at the fixture's pages directory
global.ROUTER = new Bun.FileSystemRouter({

View File

@ -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();
});

View File

@ -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 (
<AppContext.Provider value="server-context">
{children}
</AppContext.Provider>
);
}
`,
);
fs.writeFileSync(
pageFilePath,
`import { useContext } from "react";
import { AppContext } from "./__root";
export default function Page() {
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 <div>{value}</div>;
}
`,
);
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");
});
});

View File

@ -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();
});
});

View File

@ -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 }
}));
/**