mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 04:32:42 +00:00
The integration suite runs with `vitest isolate: false` (one worker,
one container — migration cost demands it). Each test file declared
its own `vi.mock("next/headers", ...)` whose factory closed over a
file-local `cookieJar` Map. Vitest resolves a mock factory ONCE per
worker — whichever file loaded first won the registration, and the
other files' Map writes silently disappeared.
That made `admin-data-wipe.test.ts` flake intermittently (~12.5% per
empirical 8-run sample) when `idempotency-replay.test.ts` happened to
load first: its `vi.mock("@/lib/auth/session", ...)` then leaked,
returning a stub user without an admin role, so `requireAdmin()`
returned 401 and the wipe assertion failed with `expected 401 to be
200`.
Fix: extract cookieJar + headerJar into module-singleton Maps in
`tests/integration/mock-next-headers.ts`. All three integration files
import them and resolve them via dynamic-import inside the
`vi.mock("next/headers", ...)` factory body, so the factory always
sees the same backing Maps regardless of which file's registration
won the race. The `vi.mock("@/lib/auth/session", ...)` stub in
idempotency-replay was also removed; the test now seeds a real
Session row + cookie like the other two files.
After: 10/10 consecutive `pnpm test:integration` runs green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
21 lines
1001 B
TypeScript
21 lines
1001 B
TypeScript
/**
|
|
* Shared `next/headers` mock state for the integration suite.
|
|
*
|
|
* Background: vitest runs the integration suite with `isolate: false`
|
|
* (one worker, one container). The factory passed to
|
|
* `vi.mock("next/headers", ...)` is resolved ONCE per worker —
|
|
* whichever test file loads first wins. If two files each declare a
|
|
* top-level `const cookieJar = new Map()` and reference it from their
|
|
* own `vi.mock("next/headers", ...)` factory, only the first file's
|
|
* Map is ever read; the second file's writes silently disappear and
|
|
* its tests flake based on import order.
|
|
*
|
|
* Fix: every integration file imports `cookieJar` + `headerJar` from
|
|
* THIS module (a singleton) and clears them in `beforeEach`. The
|
|
* `vi.mock` factory inside each test file resolves the Maps via
|
|
* `await import()` so it sees the same Map instances regardless of
|
|
* which file's mock factory ran first.
|
|
*/
|
|
export const cookieJar = new Map<string, string>();
|
|
export const headerJar = new Map<string, string>();
|