import "@testing-library/jest-dom/vitest"; import { createRequire, syncBuiltinESMExports } from "node:module"; import { cleanup, configure } from "@testing-library/react"; import { afterEach, beforeEach, vi } from "vitest"; import { DEFAULT_TEST_TIMEOUT_MS } from "./helpers/child-process-timeout.ts"; // findBy*/waitFor settle on the DOM change itself; their deadline only reports // a hang. Routes render through React.lazy, and React 19 holds every Suspense // reveal on a real 300ms timer (FALLBACK_THROTTLE_MS). A route entered through // a context table and then a lazy page spends 600ms of the 1s default before // any rendering work, so on a loaded host the default turns into a speed // requirement. Fake timers cannot shorten it: Testing Library advances only // Jest's fake timers, and MSW and user-event run on the real clock. Half the // 10s test timeout still reports a real hang with the DOM dump. configure({ asyncUtilTimeout: 5_000 }); afterEach(() => { cleanup(); // A test that fails before its own vi.useRealTimers() must not leave fake // timers to the next test in the file. vi.useRealTimers(); }); /** * A test that starts a child process declares its own timeout * (docs/work/reference/testing.md 2절). On a loaded desktop a 0.7 s * documentation test took 10.6 s and failed FE-GATE-020 on the default, which * is sized for tests that run in process. A test still on the default fails * here, at the call, also when a helper or a script it imports starts the * process. Hooks are not checked: they have no test of their own. */ type ChildProcessCall = (this: unknown, ...args: unknown[]) => unknown; const guard = globalThis as typeof globalThis & { __childProcessTimeoutGuard?: { timeout: number | undefined }; }; if (!guard.__childProcessTimeoutGuard) { const state: { timeout: number | undefined } = { timeout: undefined }; guard.__childProcessTimeoutGuard = state; const childProcess = createRequire(import.meta.url)("node:child_process") as Record; for (const name of ["spawn", "spawnSync", "exec", "execSync", "execFile", "execFileSync", "fork"]) { const original = childProcess[name]!; const checked: ChildProcessCall = function (this: unknown, ...args: unknown[]) { if (state.timeout === DEFAULT_TEST_TIMEOUT_MS) { throw new Error( `${name}(${String(args[0])}) ran in a test on the default ${DEFAULT_TEST_TIMEOUT_MS} ms timeout. ` + "Give the test, or its describe, childProcessTimeout() from tests/helpers/child-process-timeout.ts.", ); } return original.apply(this, args); }; // util.promisify(execFile) and the like read properties of the function. for (const key of Reflect.ownKeys(original)) { if (key !== "length" && key !== "name" && key !== "prototype") { Object.defineProperty(checked, key, Reflect.getOwnPropertyDescriptor(original, key)!); } } childProcess[name] = checked; } syncBuiltinESMExports(); } const childProcessGuard = guard.__childProcessTimeoutGuard; beforeEach((context) => { childProcessGuard.timeout = context.task.timeout; }); afterEach(() => { childProcessGuard.timeout = undefined; });