Files
sencho/backend/vitest.config.ts
T
Anso 2000653fb4 perf(test): build baseline DB once via vitest globalSetup (#829)
Each test file's setupTestDb() previously re-ran the full
DatabaseService init path: initSchema (~30 CREATE TABLE IF NOT
EXISTS), 14 idempotent migrate*() methods, a bcrypt hash, and the
admin / settings seed inserts. With 82 files this was a meaningful
slice of the per-fork cold-start cost.

Move the build into a vitest globalSetup that runs once before any
worker boots. The baseline DB lands at a fixed temp path; each
worker's setupTestDb copies it into the per-file data dir, opens the
copy via DatabaseService.getInstance() (re-running the same
idempotent init as a no-op pass), then UPDATEs the seeded local
node's compose_dir to match the per-file COMPOSE_DIR (the baseline
recorded /app/compose because COMPOSE_DIR was unset when the seed
fired in initSchema; without realigning, file-routes tests 400 on
path traversal).

TEST_JWT_SECRET moves from a per-file randomBytes assignment to a
fixed constant in a new testConstants module so the value the
baseline seeds matches the value test files import for direct token
signing. setupTestDb re-exports it for back-compat with the existing
import sites.

A baseline-less measurement on the same machine flakes 30 of 82
files at the no-cap baseline; with this baseline copy, the same
tree drops to 0-3 failures (the residual environmental Windows
flakes) and ~47-52 s wall time.
2026-04-28 10:08:39 -04:00

32 lines
1.4 KiB
TypeScript

import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
environment: 'node',
// Only run TypeScript sources - exclude the compiled dist/ output.
include: ['src/__tests__/**/*.test.ts'],
exclude: ['dist/**', 'node_modules/**'],
// Build the baseline DB (schema + migrations + admin seed) once; each
// test file's setupTestDb copies it instead of re-running migrations.
globalSetup: ['./src/__tests__/helpers/vitestGlobalSetup.ts'],
// Each test file gets its own worker so singletons are fresh between files.
pool: 'forks',
// Cap concurrency: each worker dynamic-imports the full Express stack
// (TypeScript transform + DB init + every migration), so an uncapped
// fork pool that scales with availableParallelism saturates CPU and
// the suite spends most of its wall time waiting on cold-start
// contention. Four parallel workers is a sweet spot on both CI
// runners and laptops.
maxWorkers: 4,
minWorkers: 1,
// Timeout generous for DB init and HTTP calls.
testTimeout: 30_000,
// Every test file dynamic-imports the full Express stack; each fork pays
// TypeScript transformation cost and can take tens of seconds under
// CPU contention.
hookTimeout: 45_000,
// Sequential within each file (DB state is shared per file).
sequence: { concurrent: false },
},
});