Files
goodgo-platform/e2e/global-setup.ts
2026-05-07 13:08:20 +07:00

57 lines
2.0 KiB
TypeScript

import { execSync } from 'node:child_process';
import path from 'node:path';
/**
* Playwright globalSetup — runs once before all E2E tests.
*
* 1. Loads .env.test (if present) so DATABASE_URL points to the test DB.
* 2. Runs Prisma migrations against the test database.
* 3. Seeds the test database with sample data.
*/
export default async function globalSetup() {
const root = path.resolve(__dirname, '..');
const envTestPath = path.join(root, '.env.test');
const isCI = !!process.env.CI;
// In CI, env vars are already set by the workflow; locally, load .env.test
if (!isCI) {
const { config } = await import('dotenv');
config({ path: envTestPath, override: true });
}
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
throw new Error(
'DATABASE_URL is not set. Create .env.test or set it in your environment.',
);
}
// In CI, the workflow already runs migrations + seed before Playwright.
// Skip to avoid duplicate work; only validate DATABASE_URL is set.
if (isCI) {
console.log('[E2E globalSetup] CI detected — skipping migrations/seed (handled by workflow).');
return;
}
console.log('\n[E2E globalSetup] Preparing test database...');
console.log(`[E2E globalSetup] DATABASE_URL = ${databaseUrl.replace(/\/\/.*@/, '//***@')}`);
const execOpts = {
cwd: root,
stdio: 'inherit' as const,
env: { ...process.env, DATABASE_URL: databaseUrl },
};
// Apply committed migrations only. `db push --accept-data-loss` hides
// migration drift and can mutate the test schema outside review.
console.log('[E2E globalSetup] Applying test database migrations...');
execSync('npx prisma migrate deploy --config prisma/prisma.config.ts', execOpts);
// Seed database (upserts are idempotent)
console.log('[E2E globalSetup] Seeding test database...');
execSync('npx prisma db seed --config prisma/prisma.config.ts', execOpts);
console.log('[E2E globalSetup] Test database ready.\n');
}