@@ -2,43 +2,69 @@ const NodeEnvironment = require('jest-environment-node').default
22const randomString = require ( 'randomstring' )
33const util = require ( 'util' )
44const exec = util . promisify ( require ( 'child_process' ) . exec )
5- const { PrismaClient } = require ( '@prisma/client' )
5+ const { PrismaClient } = require ( './generated/client' )
6+ const { PrismaPg } = require ( '@prisma/adapter-pg' )
67
78class PrismaTestEnvironment extends NodeEnvironment {
89 constructor ( config ) {
910 super ( config )
1011
11- // Generate a unique schema identifier for this test context
12- this . schema = `test_${ randomString . generate ( {
12+ // Generate a unique database identifier for this test context
13+ this . databaseName = `test_${ randomString . generate ( {
1314 length : 16 ,
1415 charset : 'alphanumeric' ,
1516 capitalization : 'lowercase' ,
1617 } ) } `
1718
18- // Generate the pg connection string for the test schema
19- this . databaseUrl = 'postgres://postgres:password@localhost:5432/testing'
19+ // Generate the pg connection string for the test database
20+ this . databaseUrl = `postgres://postgres:password@localhost:5432/${ this . databaseName } `
21+
22+ // Set the environment variable early so imports use the correct URL
2023 process . env . DB_URL = this . databaseUrl
2124 this . global . process . env . DB_URL = this . databaseUrl
22- this . client = new PrismaClient ( )
2325 }
2426
2527 async setup ( ) {
26- // Set the required environment variable to contain the connection string
27- // to our database test schema
28- const url = `${ this . databaseUrl } ?schema=${ this . schema } `
29- process . env . DB_URL = url
30- this . global . process . env . DB_URL = url
28+ // Connect to the default postgres database to create the test database
29+ const adminUrl = 'postgres://postgres:password@localhost:5432/postgres'
30+ const adminAdapter = new PrismaPg ( { connectionString : adminUrl } )
31+ const adminClient = new PrismaClient ( { adapter : adminAdapter } )
32+
33+ // Create the test database
34+ await adminClient . $executeRawUnsafe ( `CREATE DATABASE "${ this . databaseName } "` )
35+ await adminClient . $disconnect ( )
36+
37+ // Create the client for the test database
38+ const adapter = new PrismaPg ( { connectionString : this . databaseUrl } )
39+ this . client = new PrismaClient ( { adapter } )
40+
41+ // Ensure PostGIS extension exists
42+ await this . client . $executeRawUnsafe ( `CREATE EXTENSION IF NOT EXISTS postgis` )
43+
44+ // Run migrations before the tests start
45+ const { stdout, stderr } = await exec ( 'npx prisma migrate deploy' )
46+ if ( stderr && ! stderr . includes ( 'warn' ) ) {
47+ console . error ( 'Migration error:' , stderr )
48+ }
49+ if ( stdout ) {
50+ console . log ( 'Migration output:' , stdout )
51+ }
3152
32- await exec ( 'yarn prisma migrate deploy' )
3353 return super . setup ( )
3454 }
3555
3656 async teardown ( ) {
37- // Drop the schema after the tests have completed
38- await this . client . $executeRawUnsafe (
39- `drop schema if exists "${ this . schema } " cascade` ,
40- )
41- await this . client . $disconnect ( )
57+ // Drop the database after the tests have completed
58+ if ( this . client ) {
59+ await this . client . $disconnect ( )
60+ }
61+
62+ // Connect to the default postgres database to drop the test database
63+ const adminUrl = 'postgres://postgres:password@localhost:5432/postgres'
64+ const adminAdapter = new PrismaPg ( { connectionString : adminUrl } )
65+ const adminClient = new PrismaClient ( { adapter : adminAdapter } )
66+ await adminClient . $executeRawUnsafe ( `DROP DATABASE IF EXISTS "${ this . databaseName } "` )
67+ await adminClient . $disconnect ( )
4268 }
4369}
4470
0 commit comments