| 39 | * resource without allowing PGlite shutdown to erase Vitest's failure status. |
| 40 | */ |
| 41 | export default async function setup() { |
| 42 | db = await PGlite.create(); |
| 43 | await migrate(drizzle(db), { migrationsFolder: MIGRATIONS_FOLDER }); |
| 44 | |
| 45 | // PGlite is single-session; pglite-socket multiplexes connections onto it |
| 46 | // by queueing whole transactions. Two connections let a test open two |
| 47 | // transactions and interleave them (the mirror's scan-vs-feeder race in |
| 48 | // auth/workos-mirror.node.test.ts); a third would be refused at once. |
| 49 | server = new PGLiteSocketServer({ db, port: PORT, host: "127.0.0.1", maxConnections: 2 }); |
| 50 | await server.start(); |
| 51 | |
| 52 | // eslint-disable-next-line no-console |
| 53 | console.log(`[test-db] PGlite socket server listening on 127.0.0.1:${PORT}`); |
| 54 | |
| 55 | return async () => { |
| 56 | // PGlite sets an internal 99 sentinel on startup and replaces it with 0 on |
| 57 | // close. Preserve Vitest's failure status across that close so |
| 58 | // teardown can turn neither a red test green nor a green test red. |
| 59 | const testsFailed = process.exitCode === 1; |
| 60 | await server?.stop(); |
| 61 | await db?.close(); |
| 62 | if (testsFailed) process.exitCode = 1; |
| 63 | }; |
| 64 | } |