Files
sass-mohfarawati/tests/helpers/global-db-setup.ts
T
Moh e2e06be86e test: add comprehensive automated test coverage
- Vitest multi-project setup (unit / integration / component)
- Real-Postgres integration harness via in-process PGlite (TEST_DATABASE_URL
  override), migrations applied per worker; production code untouched
- Unit: routing, locale, validation/Zod schemas, metadata, mail, site-theme,
  marquee, media, portfolio helpers, plus architecture-rule tests
- Integration: Prisma data layer, API routes, and all server actions
- Component: UI primitives and form components (jsdom + Testing Library)
- 371 tests passing
2026-08-06 02:27:20 +02:00

36 lines
1.1 KiB
TypeScript

import { readFileSync, readdirSync } from "fs";
import path from "path";
/**
* Global integration setup.
*
* Only needed when running against a real Postgres via TEST_DATABASE_URL: reset the
* schema and apply every migration once before the workers start. When
* TEST_DATABASE_URL is not set, each worker spins up its own in-process PGlite database
* (see tests/helpers/integration-setup.ts) and this is a no-op.
*/
const MIGRATIONS_DIR = path.resolve(process.cwd(), "prisma", "migrations");
export default async function setup() {
const connectionString = process.env.TEST_DATABASE_URL?.trim();
if (!connectionString) {
return;
}
const pg = (await import("pg")).default;
const client = new pg.Client({ connectionString });
await client.connect();
try {
await client.query("DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public;");
const dirs = readdirSync(MIGRATIONS_DIR)
.filter((entry) => /^\d/.test(entry))
.sort();
for (const dir of dirs) {
await client.query(readFileSync(path.join(MIGRATIONS_DIR, dir, "migration.sql"), "utf8"));
}
} finally {
await client.end();
}
}