1
0
Fork 0

test(vitest): honor KEEP_TEST_DBS=0 as force-drop escape hatch

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.
This commit is contained in:
Jason Fraley 2026-10-21 13:04:15 -04:00
parent 507d0a9e5f
commit e828f50516
3 changed files with 9 additions and 6 deletions

View file

@ -61,7 +61,7 @@ Known pre-existing errors (not yours, don't widen scope to fix them): `src/colle
- **Dedicated test databases**: all tests run against databases derived from `DATABASE_URI`, never the dev database. The default mode builds them from a template: `<db>_test_template` is ensured once per schema change (`tests/test-db.ts`: ensure db, drizzle `push` with auto-accepted warnings via `tests/push-schema.ts`, analytics DDL, `seedRoles` + baseline seed, sequence repair; a broad schema-hash is cached in `.cache/testdb-template.json` and warm runs skip push/seed entirely), then vitest forks clone it into `<db>_test_w<ID>` at startup (`CREATE DATABASE ... TEMPLATE`; per-fork attach in `tests/vitest-worker-setup.ts`). Two forks never share tables, so files run sequentially-within-a-fork and in parallel across forks (pool `forks`, `isolate: false`, which is required for worker reuse and for the memoized `tests/helpers/get-test-payload.ts` boot). Teardown (returned by `tests/vitest-global.ts`; Vitest 4 removed the `globalTeardown` config) drops clones after green runs and keeps them after failures for postmortem; leftover clones from crashed runs are dropped at the next startup. E2E keeps the single serial `<db>_test`, recreated fresh from the template each run, then seeded with the shared e2e login. Environment flag matrix:
- `TEST_DATABASE_URI`: forces legacy serial mode against exactly this database (truncate-per-run, no templates/clones, `fileParallelism: false`), the documented bisect hatch for flaky specs.
- `TEST_DB_HARD_RESET=1`: ignores the template cache and rebuilds `<db>_test_template` from scratch.
- `KEEP_TEST_DBS=1`: teardown keeps worker clones even after green runs.
- `KEEP_TEST_DBS=1`: teardown keeps worker clones even after green runs; `KEEP_TEST_DBS=0` forces dropping even after failures.
- `VITEST_MAX_FORKS`: fork cap (default 8).
- `TEST_PG_POOL_MAX`: internal (set by worker setup, default 8), caps the node-postgres pool per Payload boot in workers mode so N forks stay under `max_connections=100`.
- **Integration tests**: `tests/int/**/*.int.spec.ts` - Vitest, default `node` environment with component specs opting in via a `// @vitest-environment jsdom` docblock. Requires the live PostgreSQL test databases (bootstrapped automatically via `vitest-global.ts`; `vitest.setup.ts` points the process at it). Uses `dotenv/config` via `vitest.setup.ts`.

View file

@ -43,7 +43,7 @@ import "dotenv/config";
* - `TEST_DB_HARD_RESET=1`: ignore the template cache and rebuild the
* template from scratch.
* - `KEEP_TEST_DBS=1`: teardown keeps worker clones even after green runs
* (postmortem hatch).
* (postmortem hatch); `KEEP_TEST_DBS=0` forces dropping even after failures.
*/
/** Tables that must survive truncation: PostGIS internals and migration journals. */

View file

@ -8,9 +8,10 @@ import { failureMarkerPresent } from "./vitest-marker";
* 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`. The
* template itself survives across runs and is only rebuilt on hash mismatch
* or `TEST_DB_HARD_RESET=1`.
* 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> {
@ -19,7 +20,9 @@ export default async function vitestTeardown(): Promise<void> {
await truncateAllData(resolveTestDatabaseUri());
return;
}
if (process.env.KEEP_TEST_DBS === "1" || failureMarkerPresent()) {
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;
}