import { execFile, spawnSync } from "node:child_process"; import { promisify } from "node:util"; import { describe, expect, it } from "vitest"; import config from "../../vitest.config.ts"; import { childProcessTimeout, DEFAULT_TEST_TIMEOUT_MS } from "../helpers/child-process-timeout.ts"; const NODE_ARGS = ["-e", "process.stdout.write('ran')"]; // tests/setup.ts fails a test on the default timeout that starts a child process. describe("a child process in a test on the default timeout", () => { it("fails at the call, naming the call and the helper", () => { expect(() => spawnSync(process.execPath, NODE_ARGS)).toThrow( /spawnSync\(.+\) ran in a test on the default 10000 ms timeout\. .*childProcessTimeout/u, ); expect(() => execFile(process.execPath, NODE_ARGS)).toThrow(/execFile\(.+\) ran in a test on the default/u); }); it("is the default vitest.config.ts sets", () => { expect(config.test?.testTimeout).toBe(DEFAULT_TEST_TIMEOUT_MS); }); }); describe("a child process in a test with its own timeout", { timeout: childProcessTimeout(100) }, () => { it("runs", async () => { expect(spawnSync(process.execPath, NODE_ARGS, { encoding: "utf8" }).stdout).toBe("ran"); // The guard keeps what util.promisify reads from the original function. await expect(promisify(execFile)(process.execPath, NODE_ARGS)).resolves.toEqual({ stdout: "ran", stderr: "" }); }); }); describe("childProcessTimeout", () => { it("is fifteen times the longest measured run, rounded up to 30 s steps", () => { expect(childProcessTimeout(0)).toBe(30_000); expect(childProcessTimeout(1_999)).toBe(30_000); expect(childProcessTimeout(2_001)).toBe(60_000); expect(childProcessTimeout(5_739)).toBe(90_000); }); });