diff --git a/test/main.test.ts b/test/main.test.ts index 01bffde..a1d8634 100644 --- a/test/main.test.ts +++ b/test/main.test.ts @@ -16,36 +16,42 @@ process.env.REVIEWS_ENABLED = "true"; const app = await import("../src/main").then(m => m.default); const PGSQL_IMAGE = "postgres:latest"; -let postgresContainer: StartedTestContainer; +let postgresContainer: StartedTestContainer | null = null; let jwk: JWK; let privateKey: string; // Spin up a temporary Postgres container for testing beforeAll(async () => { - postgresContainer = await new GenericContainer(PGSQL_IMAGE) - .withEnvironment({ - POSTGRES_USER: "tc", - POSTGRES_PASSWORD: "tc", - POSTGRES_DB: "tc" - }) - .withExposedPorts(5432) - .withHealthCheck({ - test: ["CMD-SHELL", "pg_isready -U postgres"], - interval: 1000, - timeout: 3000, - retries: 5, - }) - .withWaitStrategy(Wait.forHealthCheck()) - .start(); - - process.env.DATABASE_URL = `postgres://tc:tc@localhost:${postgresContainer.getMappedPort(5432)}/tc`; + if(!process.env.DATABASE_URL) { + postgresContainer = await new GenericContainer(PGSQL_IMAGE) + .withEnvironment({ + POSTGRES_USER: "tc", + POSTGRES_PASSWORD: "tc", + POSTGRES_DB: "tc" + }) + .withExposedPorts(5432) + .withHealthCheck({ + test: ["CMD-SHELL", "pg_isready -U postgres"], + interval: 1000, + timeout: 3000, + retries: 5, + }) + .withWaitStrategy(Wait.forHealthCheck()) + .start(); + + process.env.DATABASE_URL = `postgres://tc:tc@localhost:${postgresContainer.getMappedPort(5432)}/tc`; + } getDb(true); await initDb(); }); afterAll(async () => { - await postgresContainer.stop(); + // Nuke the entire database after tests + const db = getDb(); + await db.dropDatabase(); + await db.destroy(); + if(postgresContainer) await postgresContainer.stop(); }); // Override JWKS URL to prevent external calls during tests