import "@testing-library/jest-dom"; import { configure } from "@testing-library/react"; import { vi } from "vitest"; // The Docker image runs this suite beside `next build` (parallel-portal-test-stage), so a test can // wait well past Testing Library's 1 s default for async UI on a busy CPU. A longer limit costs // nothing when the UI appears: findBy and waitFor resolve as soon as it does. configure({ asyncUtilTimeout: 5000 }); // vitest has no next/font transform; stub the loader. vi.mock("next/font/local", () => ({ default: (o: { variable: string }) => ({ variable: o.variable, className: "x" }), })); // Page tests are about page behaviour, not sizing: pin the fitted page size to 20. // app/lib/hooks/useFitPageSize.test.tsx unmocks it to test the real measurement. vi.mock("@/app/lib/hooks/useFitPageSize", async (importOriginal) => ({ ...(await importOriginal()), useFitPageSize: () => ({ tableRef: () => {}, pageSize: 20 }), })); // localise-admin-shell: server components call getServerT(), which reads next/headers. Suites that do not // render them get the English defaults; app/lib/server/i18n.test.ts and app/layout.test.tsx vi.unmock it. vi.mock("@/app/lib/server/i18n", async () => { const { resolveMessage } = await import("@/app/lib/i18n/t"); return { requestLocale: async () => ({ locale: "en_US", locales: [] }), requestCatalog: async () => ({}), getServerT: async () => (code: string, params?: Record) => resolveMessage({}, code, params), }; }); // Everything below patches the DOM, which only the jsdom project has. if (typeof window !== "undefined") { // Responsive components (the shadcn sidebar's mobile breakpoint) need matchMedia. Object.defineProperty(window, "matchMedia", { writable: true, value: (query: string) => ({ matches: false, media: query, onchange: null, addListener: () => {}, removeListener: () => {}, addEventListener: () => {}, removeEventListener: () => {}, dispatchEvent: () => false, }), }); // Recharts' ResponsiveContainer and Radix popups observe size changes. class ResizeObserverStub { observe() {} unobserve() {} disconnect() {} } window.ResizeObserver = window.ResizeObserver ?? (ResizeObserverStub as unknown as typeof ResizeObserver); // Radix primitives (Select, DropdownMenu) drive interaction through the Pointer // Events API and scroll the active item into view. jsdom implements neither, so // without these stubs every dropdown interaction throws. Element.prototype.hasPointerCapture = Element.prototype.hasPointerCapture ?? (() => false); Element.prototype.setPointerCapture = Element.prototype.setPointerCapture ?? (() => {}); Element.prototype.releasePointerCapture = Element.prototype.releasePointerCapture ?? (() => {}); Element.prototype.scrollIntoView = Element.prototype.scrollIntoView ?? (() => {}); } // sign-session-cookies (F32): a fixed session signing key for tests. process.env.SESSION_SECRET ??= "vitest-session-secret";