(examplePath: string, options?: TestExampleOptions)
| 13 | } |
| 14 | |
| 15 | export function testExample(examplePath: string, options?: TestExampleOptions) { |
| 16 | describe(examplePath, () => { |
| 17 | let server: Server |
| 18 | |
| 19 | afterAll(async () => { |
| 20 | await server?.close() |
| 21 | }) |
| 22 | |
| 23 | test('prisma setup', async () => { |
| 24 | server = await startPrismaDevServer({ databaseIdleTimeoutMillis: 300000 }) |
| 25 | const url = server.database.connectionString |
| 26 | const databaseUrl = url.startsWith('postgres://') |
| 27 | ? url.replace('postgres://', 'postgresql://') |
| 28 | : url |
| 29 | const cwd = path.join(process.cwd(), examplePath) |
| 30 | const env = { ...process.env, DATABASE_URL: databaseUrl, DIRECT_URL: databaseUrl } |
| 31 | const packageJsonPath = path.join(cwd, 'package.json') |
| 32 | const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) |
| 33 | const scripts = packageJson.scripts ?? {} |
| 34 | |
| 35 | console.log(`\n[${examplePath}] Installing dependencies...`) |
| 36 | await execa('npm', ['install'], { cwd, env, stdio: 'inherit' }) |
| 37 | |
| 38 | console.log(`\n[${examplePath}] Running prisma generate...`) |
| 39 | await execa('npx', ['prisma', 'generate'], { cwd, env, stdio: 'inherit' }) |
| 40 | |
| 41 | if (!options?.skipMigrate) { |
| 42 | console.log(`\n[${examplePath}] Running prisma db push...`) |
| 43 | await execa('npx', ['prisma', 'db', 'push', '--accept-data-loss'], { |
| 44 | cwd, |
| 45 | env, |
| 46 | stdio: 'inherit', |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | // Check for seed in prisma.config.ts (Prisma v7+) |
| 51 | const configPath = path.join(cwd, 'prisma.config.ts') |
| 52 | if (!options?.skipSeed && fs.existsSync(configPath)) { |
| 53 | const content = fs.readFileSync(configPath, 'utf-8') |
| 54 | if (content.includes('seed:')) { |
| 55 | console.log(`\n[${examplePath}] Running prisma db seed...`) |
| 56 | await execa('npx', ['prisma', 'db', 'seed'], { cwd, env, stdio: 'inherit' }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (options?.runBuild && scripts.build) { |
| 61 | console.log(`\n[${examplePath}] Running build...`) |
| 62 | await execa('npm', ['run', 'build'], { cwd, env, stdio: 'inherit' }) |
| 63 | } |
| 64 | |
| 65 | console.log(`\n[${examplePath}] Completed successfully!`) |
| 66 | }) |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | // For SQLite examples that don't need @prisma/dev server |
| 71 | export function testSqliteExample( |
no test coverage detected