Plan wording kept clones on failure unless KEEP_TEST_DBS=0; teardown now implements both ends explicitly (=1 always keep, =0 always drop, default keep-on-failure). Verified with a deliberately failing spec both ways.
33 lines
1.4 KiB
TypeScript
33 lines
1.4 KiB
TypeScript
import { dropStaleWorkerDatabases, resolveTestDatabaseUri, truncateAllData } from "./test-db";
|
|
import { failureMarkerPresent } from "./vitest-marker";
|
|
|
|
/**
|
|
* Post-run cleanup returned by tests/vitest-global.ts (Vitest 4 removed the
|
|
* `globalTeardown` config option entirely; the supported contract is that
|
|
* globalSetup returns this function). Failures here must never fail the run;
|
|
* leftovers are dropped by the next run's globalSetup sweep regardless.
|
|
*
|
|
* - Workers mode: drop every `<base>_w<N>` clone after a green run; keep them
|
|
* when the failure marker exists (postmortem) or `KEEP_TEST_DBS=1`.
|
|
* `KEEP_TEST_DBS=0` forces dropping even after failures. The template
|
|
* itself survives across runs and is only rebuilt on hash mismatch or
|
|
* `TEST_DB_HARD_RESET=1`.
|
|
* - Legacy serial mode (`TEST_DATABASE_URI` set): truncate for hygiene.
|
|
*/
|
|
export default async function vitestTeardown(): Promise<void> {
|
|
try {
|
|
if (process.env.TEST_DATABASE_URI) {
|
|
await truncateAllData(resolveTestDatabaseUri());
|
|
return;
|
|
}
|
|
const forceKeep = process.env.KEEP_TEST_DBS === "1";
|
|
const forceDrop = process.env.KEEP_TEST_DBS === "0";
|
|
if (!forceDrop && (forceKeep || failureMarkerPresent())) {
|
|
console.log("[test-db] keeping worker clones (failed run or KEEP_TEST_DBS=1)");
|
|
return;
|
|
}
|
|
await dropStaleWorkerDatabases();
|
|
} catch (error) {
|
|
console.warn("[test-db] teardown cleanup failed:", error);
|
|
}
|
|
}
|