Root-cause fix for cross-file DB pollution in the vitest suite, which surfaced as intermittent FK-violation failures once the suite hit 12 test files. tests/setup.ts previously mutated the global process.env.DATABASE_URL and relied on src/lib/prisma.ts's singleton picking it up, which is racy across files sharing a worker process (process.env isn't reset between files even though Vitest resets each file's module registry). fileParallelism: false only masked this. Now each test file's own tests/setup.ts creates its own throwaway Postgres database and vi.mock()s "@/lib/prisma" with a dedicated PrismaClient constructed via the explicit datasourceUrl option, so no mutable state is shared between files and fileParallelism can go back to its default (true). Verified with 4 consecutive full `npm test` runs (327/327 passing each time) and no leftover throwaway databases after each run.
32 lines
1.4 KiB
TypeScript
32 lines
1.4 KiB
TypeScript
import { defineConfig } from "vitest/config";
|
|
import path from "node:path";
|
|
|
|
export default defineConfig({
|
|
resolve: {
|
|
alias: {
|
|
"@": path.resolve(__dirname, "./src"),
|
|
},
|
|
},
|
|
test: {
|
|
environment: "node",
|
|
// Scope explicitly to this repo's own tests/ dir — without this, Vitest's
|
|
// default glob also picks up any stray sibling git worktree checked out
|
|
// under .claude/worktrees/ (e.g. worktree-ui-basic), which isn't excluded
|
|
// by Vitest's defaults since those only skip node_modules/.git/etc, not
|
|
// .claude/. A stray worktree's stale test file still resolves the "@"
|
|
// alias to *this* repo's src/ (see resolve.alias above, which is
|
|
// __dirname-relative), so it silently runs old tests against current
|
|
// code under a shared identity — confirmed happening in practice once a
|
|
// second worktree existed.
|
|
include: ["tests/**/*.test.ts"],
|
|
setupFiles: ["./tests/setup.ts"],
|
|
// Each test file gets its own throwaway Postgres database and its own
|
|
// dedicated PrismaClient, wired up via a `vi.mock("@/lib/prisma", ...)`
|
|
// factory in tests/setup.ts (not by mutating the ambient
|
|
// process.env.DATABASE_URL, which used to be shared/racy across files —
|
|
// see the long comment at the top of tests/setup.ts for the history).
|
|
// With no mutable state shared between files, parallel file execution
|
|
// (Vitest's default) is safe again.
|
|
},
|
|
});
|