import { config as setupEnv } from 'dotenv-flow'; setupEnv({ silent: true }); import NodeEnvironment from 'jest-environment-node'; import { Client } from 'pg'; import { v4 as uuidv4 } from 'uuid'; import { createDatabase, teardown } from './db'; class TestEnvironmentWithDatabases extends NodeEnvironment { private dbname?: string; private testClient?: Client; async setup() { await super.setup(); this.dbname = `testdb-${uuidv4()}`; this.testClient = await createDatabase(this.dbname); this.global.TYPEORM_DATABASE = this.dbname; } async teardown() { await super.teardown(); if (this.testClient && this.dbname) { await teardown(this.testClient, this.dbname); } } } module.exports = TestEnvironmentWithDatabases;