1
0
Fork 0
polaris-task-force/tests/vitest-worker-setup.ts
Z8MB1E cc8a911502 test(vitest): parallel forks with per-worker database clones
Stage 2 of the int-suite speedup: pool=forks with fileParallelism and a
default cap of 8 (VITEST_MAX_FORKS), each fork cloning the template into
ptf_*_test_w<ID> via tests/vitest-worker-setup.ts; environment defaults
to node with jsdom opt-in docblocks on the six DOM-consuming specs; a
tiny reporter writes a failure marker so post-run cleanup keeps clones
after red runs (KEEP_TEST_DBS=1 forces keeping); pg pools cap at
TEST_PG_POOL_MAX=3 per boot in workers mode. Legacy serial mode stays
reachable by setting TEST_DATABASE_URI.

Notes from the gate G2 runs (Vitest 4.0.16): globalTeardown config was
removed in Vitest 4 entirely - cleanup now returns from globalSetup. The
pool scheduler only reuses worker processes when isolate:false, so this
per-file-fork stage lands short of the wall-time target and is expected
to be superseded by the gated isolate:false stage. Gate G2 warm serial
equivalent stays green via TEST_DATABASE_URI bisect mode.
2026-10-21 10:41:57 -04:00

44 lines
2 KiB
TypeScript

import { cloneTemplateInto, workerDatabaseUri } from "./test-db";
/**
* Per-fork vitest setup, appended AFTER vitest.setup.ts in setupFiles. Gives
* every fork its own clone of the template database (`<base>_w<ID>`),
* repoints DATABASE_URI at it, and caps the pg pool per Payload boot so N
* forks stay far below Postgres' max_connections.
*
* No-ops outside workers mode: with `TEST_DATABASE_URI` set the suite runs in
* legacy serial mode against the single override db (bisect hatch), and a
* missing VITEST_WORKER_ID means this is not a vitest worker process.
*
* NOTE: vitest imports setupFiles but never calls their default export, so
* the work happens in a top-level IIFE below. The vite-node SSR transform
* hoists top-level await into an async wrapper, so side effects MUST live
* inside the awaited function, not before it.
*
* The clone happens ONCE PER FORK, not per test file: setup files re-run
* before every file under isolate:true, but process.env survives those module
* resets, so the marker env var short-circuits repeats. Within a fork, files
* keep running sequentially against one database that started empty plus the
* seeded baseline, exactly mirroring the old cross-file semantics while two
* forks never share tables.
*/
const CLONED_MARKER_ENV = "PTF_WORKER_DB_CLONED";
async function attachWorkerDatabase(): Promise<void> {
if (process.env.TEST_DATABASE_URI || !process.env.VITEST_WORKER_ID) return;
// vitest.setup.ts already repointed DATABASE_URI at the serial test db, so
// derive names from THAT (re-resolving would append `_test` twice).
const uri = workerDatabaseUri(process.env.VITEST_WORKER_ID, process.env.DATABASE_URI);
if (process.env[CLONED_MARKER_ENV] === uri) return;
await cloneTemplateInto(uri);
process.env[CLONED_MARKER_ENV] = uri;
process.env.DATABASE_URI = uri;
if (!process.env.TEST_PG_POOL_MAX) {
// Reads as pg PoolConfig.max in src/payload.config.ts (unset elsewhere).
process.env.TEST_PG_POOL_MAX = "3";
}
}
await attachWorkerDatabase();