import { execFileSync } from 'node:child_process'; import { PrismaClient } from '@prisma/client'; /** * Database-backed tests run only when TEST_DATABASE_URL is set (locally: * the compose dev db on port 5434; in CI: the postgres service container). * This setup pushes the current Prisma schema into that database once per * test run; tests skip themselves when the variable is absent. */ export default async function globalSetup(): Promise { const url = process.env.TEST_DATABASE_URL; if (!url) return; execFileSync( process.execPath, [require.resolve('prisma/build/index.js'), 'db', 'push', '--skip-generate'], { env: { ...process.env, DATABASE_URL: url }, stdio: 'inherit', cwd: __dirname }, ); // The shared test database counts as a configured instance — without the // completion marker every suite would hit the first-run setup gate // (issue #80). The setup suite provisions its own fresh database instead. const prisma = new PrismaClient({ datasourceUrl: url }); try { await prisma.instanceSetting.upsert({ where: { key: 'setup.completedAt' }, create: { key: 'setup.completedAt', value: new Date().toISOString() }, update: {}, }); } finally { await prisma.$disconnect(); } }