// Any setup scripts you might need go here // Load .env files import "dotenv/config"; import { afterEach, vi } from "vitest"; import { resolveTestDatabaseUri } from "./tests/test-db"; // Tests never touch the development database: every payload connection in the // test process targets the dedicated test database (see tests/test-db.ts). // Must run before any test file imports the Payload config. // // Setup files re-run before EVERY test file (even under isolate:false), while // tests/vitest-worker-setup.ts repoints DATABASE_URI at a per-fork clone - // so both must be idempotent. The serial test URI is resolved ONCE per // process (PTF_SERIAL_TEST_DATABASE_URI); once a fork has cloned its own db, // the worker setup owns DATABASE_URI and we leave it alone. Re-appending // `_test` to an already-derived name here caused `_w7_test_test...` chaos. if (!process.env.PTF_SERIAL_TEST_DATABASE_URI) { process.env.PTF_SERIAL_TEST_DATABASE_URI = resolveTestDatabaseUri(); } if (!process.env.PTF_WORKER_DB_CLONED) { process.env.DATABASE_URI = process.env.PTF_SERIAL_TEST_DATABASE_URI; } // Stage 3 shared-isolation guard: under `isolate: false`, fake timers left // running by one file's tests would bleed into later files on the same fork. // NOTE: intentionally NOT calling vi.restoreAllMocks() here - it resurrects // vi.mock() module factories (e.g. "next/headers") that specs depend on for // their remaining tests within a file; observed breaking map-system and // supply-box-actions when applied globally after every test. afterEach(() => { vi.useRealTimers(); }); // Fire-and-forget game-event/notification writes can reject AFTER their test // file has cleaned up fixture rows (FK on actor_id), which Vitest 4 reports // as an anonymous global unhandled rejection and turns the whole run red even // though every test passed. The emitting code paths already catch these in // most orderings; this listener makes that explicit for the racy remainder: // log for visibility, and mark handled so a benign post-cleanup race cannot // flip the exit code. Genuine assertion/hook failures still fail normally. process.on("unhandledRejection", (reason) => { const r = reason as { message?: string; stack?: string; cause?: { message?: string } }; console.error( `[test-db] swallowed post-cleanup unhandled rejection: ${r?.message ?? r?.cause?.message ?? JSON.stringify(reason)?.slice(0, 200)}`, ); });