docs: rewrite test-database lifecycle section for template/clone workers
Stage 4: document template + per-fork clones, the env flag matrix (TEST_DATABASE_URI legacy serial bisect, TEST_DB_HARD_RESET, KEEP_TEST_DBS, VITEST_MAX_FORKS, TEST_PG_POOL_MAX), jsdom-opt-in docblocks, and the Vitest 4 returned-teardown contract.
This commit is contained in:
parent
938da9bf86
commit
507d0a9e5f
1 changed files with 7 additions and 2 deletions
|
|
@ -58,8 +58,13 @@ Known pre-existing errors (not yours, don't widen scope to fix them): `src/colle
|
|||
|
||||
## Test details
|
||||
|
||||
- **Dedicated test database**: all tests run against `<db>_test` (derived from `DATABASE_URI`, or set `TEST_DATABASE_URI`), never the dev database. `tests/test-db.ts` bootstraps it idempotently: ensure database exists, push the schema (drizzle `push`, because the migration chain assumes a schema that push created), truncate every table (migration journals and PostGIS internals survive), seed baselines (`seedRoles` + a Game Rules global with a main currency resource, `tests/seed-baseline.ts`), and repair id sequences (migrations backfill explicit-id rows, which leaves sequences behind and causes `ValidationError: field is invalid: id` on creates). Truncate runs at both global setup (clears crashed-run leftovers) and teardown, so every run starts and ends empty. `TEST_DB_HARD_RESET=1` drops and recreates the test database from scratch instead of truncating. Serialized test files (`fileParallelism: false`) because parallel workers share the one database.
|
||||
- **Integration tests**: `tests/int/**/*.int.spec.ts` — Vitest with jsdom. Requires the live PostgreSQL test database (bootstrapped automatically via `vitest-global.ts`; `vitest.setup.ts` points the process at it). Uses `dotenv/config` via `vitest.setup.ts`.
|
||||
- **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.
|
||||
- `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`.
|
||||
- **E2E tests**: `tests/e2e/*.e2e.spec.ts` — Playwright with a project named "vivaldi" that launches the **Vivaldi binary** (`/usr/bin/vivaldi`, headless args in `playwright.config.ts`) — not plain Chromium. Override the binary with `E2E_BROWSER_PATH` (e.g. Playwright's bundled chromium) when the installed Vivaldi crashes under automation; the default stays Vivaldi. The `webServer` config auto-starts `pnpm dev` with `DATABASE_URI` pointed at the test DB (bootstrapped by `tests/e2e/global-setup.ts`, with `reuseExistingServer: true`). `global-setup` also seeds the shared e2e login: `dev` / `Test123` (developer legacy role + a superuser Roles doc for permission-gated UI + the "J. Fraley" rank so the dashboard heading matches). E2E runs serially (`workers: 1`, one webpack dev server) with generous `timeout`/`expect` timeouts because first navigation to a route triggers a cold webpack compile. Currently: tours smoke (auto-start, dismissal, replay), frontend, operations, reservations, supply boxes, OPORD recall, session banner, tech tree. Caveat: when your own dev server is already on port 3000, `reuseExistingServer` silently reuses it and its DEV database, bypassing the test-DB wiring; free the port first for a true test-DB run.
|
||||
- Run a single integration test: `bun run vitest run tests/int/api.int.spec.ts`
|
||||
- Run a single e2e test: `bun run playwright test tests/e2e/frontend.e2e.spec.ts`
|
||||
|
|
|
|||
Loading…
Reference in a new issue