import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; import { parseSync } from "@babel/core"; import { describe, expect, it } from "vitest"; import { childProcessTimeout } from "../helpers/child-process-timeout.ts"; /** * 1112/03-08 (T-08). A Vitest test, describe or hook that needs more than the * default timeout takes `childProcessTimeout()` * (docs/work/reference/testing.md 2절), so the number at the call is a desktop * measurement. A number written by hand (`}, 60_000)`, `{ timeout: 30_000 }`, * a `PROCESS_HEAVY_TIMEOUT_MS = 30_000` constant, here or imported) has none, * and 54 of them were left beside the helper. tests/setup.ts fails a child * process on the default timeout at run time; this finds the hand-written * numbers in the source, hooks included. */ type SyntaxNode = { type: string; loc?: { start: { line: number } } } & Record; const TEST_APIS = new Set(["it", "test", "describe", "suite", "bench"]); const HOOKS = new Set(["beforeAll", "afterAll", "beforeEach", "afterEach"]); const HELPER = "childProcessTimeout"; function isNode(value: unknown): value is SyntaxNode { return typeof value === "object" && value !== null && typeof (value as SyntaxNode).type === "string"; } function children(node: SyntaxNode): SyntaxNode[] { return Object.entries(node).flatMap(([key, value]) => key === "loc" || key.endsWith("Comments") || key === "comments" ? [] : (Array.isArray(value) ? value : [value]).filter(isNode), ); } /** `it`, `it.concurrent`, `it.each(table)`, `describe.skipIf(x)`: the Vitest name at the root. */ function vitestApi(callee: SyntaxNode): string | null { if (callee.type === "Identifier") return callee["name"] as string; if (callee.type === "MemberExpression") return vitestApi(callee["object"] as SyntaxNode); if (callee.type === "CallExpression") return vitestApi(callee["callee"] as SyntaxNode); return null; } const isFunction = (node: SyntaxNode) => node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression"; function timeoutArgument(api: string, args: readonly SyntaxNode[]): SyntaxNode | null { const body = args.findIndex(isFunction); if (body < 0) return null; const candidates = HOOKS.has(api) ? [args[body + 1]] : [args[body + 1], body === 2 ? args[1] : undefined]; for (const candidate of candidates) { if (!candidate) continue; if (candidate.type !== "ObjectExpression") return candidate; const property = (candidate["properties"] as SyntaxNode[]).find( (entry) => entry.type === "ObjectProperty" && ((entry["key"] as SyntaxNode)["name"] === "timeout" || (entry["key"] as SyntaxNode)["value"] === "timeout"), ); if (property) return property["value"] as SyntaxNode; } return null; } const isHelperCall = (node: SyntaxNode | undefined) => node?.type === "CallExpression" && (node["callee"] as SyntaxNode).type === "Identifier" && (node["callee"] as SyntaxNode)["name"] === HELPER; /** Every timeout given to a Vitest test, describe or hook that is not the helper's. */ function handWrittenTimeouts(file: string, source: string): string[] { const tree = parseSync(source, { filename: file, babelrc: false, configFile: false, sourceType: "module", parserOpts: { plugins: ["typescript", ...(file.endsWith(".tsx") ? (["jsx"] as const) : [])] }, }) as unknown as SyntaxNode | null; if (!tree) throw new Error(`cannot parse ${file}`); const program = tree["program"] as SyntaxNode; // A file-level `const X = childProcessTimeout()` names a measured value. const measured = new Set(); for (const statement of program["body"] as SyntaxNode[]) { const declaration = statement.type === "ExportNamedDeclaration" ? (statement["declaration"] as SyntaxNode | null) : statement; if (declaration?.type !== "VariableDeclaration" || declaration["kind"] !== "const") continue; for (const declarator of declaration["declarations"] as SyntaxNode[]) { const id = declarator["id"] as SyntaxNode; if (id.type === "Identifier" && isHelperCall(declarator["init"] as SyntaxNode)) measured.add(id["name"] as string); } } const found: string[] = []; const visit = (node: SyntaxNode): void => { if (node.type === "CallExpression") { const api = vitestApi(node["callee"] as SyntaxNode); if (api !== null && (TEST_APIS.has(api) || HOOKS.has(api))) { const timeout = timeoutArgument(api, node["arguments"] as SyntaxNode[]); const named = timeout?.type === "Identifier" && measured.has(timeout["name"] as string); if (timeout && !isHelperCall(timeout) && !named) { const start = timeout["start"] as number; const end = timeout["end"] as number; found.push(`${file}:${timeout.loc!.start.line} ${api} timeout ${source.slice(start, end)}`); } } } for (const child of children(node)) visit(child); }; visit(program); return found; } async function vitestFiles(directory: string): Promise { const entries = await readdir(directory, { recursive: true, withFileTypes: true }); return entries .filter((entry) => entry.isFile() && /\.test\.tsx?$/u.test(entry.name)) .map((entry) => path.join(entry.parentPath, entry.name).split(path.sep).join("/")) .filter((file) => !file.startsWith("tests/fixtures/")) .sort(); } describe("Vitest timeouts in tests/ (1112/03-08)", () => { // It parses every Vitest file in tests/ in process: up to 13.5 s in the coverage // bundle on desktop (2026-11-13). it("come from childProcessTimeout(), never a number written by hand", async () => { const files = await vitestFiles("tests"); expect(files).toContain("tests/unit/child-process-timeout.test.ts"); const found = ( await Promise.all(files.map(async (file) => handWrittenTimeouts(file, await readFile(file, "utf8")))) ).flat(); expect( found, "give childProcessTimeout() from tests/helpers/child-process-timeout.ts (docs/work/reference/testing.md 2절)", ).toEqual([]); }, childProcessTimeout(13_453)); it("finds a number, a constant and an option given to a test, a describe or a hook", () => { const source = [ 'import { PROCESS_HEAVY_TIMEOUT_MS } from "./fixture.ts";', "const BUILD_TIMEOUT_MS = 180_000;", "const MEASURED = childProcessTimeout(2_000);", 'it("a", () => {}, 60_000);', 'it("b", async () => {}, PROCESS_HEAVY_TIMEOUT_MS);', 'describe("c", { timeout: 30_000 }, () => {});', "beforeAll(async () => {}, BUILD_TIMEOUT_MS);", 'it.each([1, 2])("d %s", async () => {}, 30_000);', 'test.concurrent("e", { timeout: 20_000 }, async () => {});', 'it("f", () => {}, { timeout: 10 * 1_000 });', "", 'it("ok 1", () => {}, childProcessTimeout(5_739));', 'describe("ok 2", { timeout: childProcessTimeout(100) }, () => {});', "beforeAll(async () => {}, MEASURED);", 'it("ok 3", () => {});', // Not a Vitest timeout: a process deadline, a call argument, a timer. 'it("ok 4", () => { spawnSync("node", [], { timeout: 2_000 }); evaluateSoak(a, b, 10_000); setTimeout(done, 1_000); });', 'it("ok 5", async () => { await screen.findByRole("heading", {}, { timeout: 1_000 }); });', ].join("\n"); expect(handWrittenTimeouts("tests/unit/x.test.ts", source)).toEqual([ "tests/unit/x.test.ts:4 it timeout 60_000", "tests/unit/x.test.ts:5 it timeout PROCESS_HEAVY_TIMEOUT_MS", "tests/unit/x.test.ts:6 describe timeout 30_000", "tests/unit/x.test.ts:7 beforeAll timeout BUILD_TIMEOUT_MS", "tests/unit/x.test.ts:8 it timeout 30_000", "tests/unit/x.test.ts:9 test timeout 20_000", "tests/unit/x.test.ts:10 it timeout 10 * 1_000", ]); }); });