import 'jest-extended'; import 'reflect-metadata'; import { Connection } from 'typeorm'; import createConnection from '../src/config/db'; import { getClient as getTestClient } from './db'; // mock bull dashboard ui jest.mock('@bull-board/api'); const connections = new Map(); const getTestConnectionKey = (): string => { return expect.getState().testPath; }; beforeEach(async () => { const testName = getTestConnectionKey(); const connection = connections.get(testName); // ensure we aren't in production if (connection && process.env.NODE_ENV !== 'production') { // drop the database and rerun all migrations between tests await connection.dropDatabase(); await connection.runMigrations(); } }); beforeAll(async () => { // setup connections for this test const testName = getTestConnectionKey(); let connection = connections.get(testName); if (!connection) { connection = await createConnection(); connections.set(testName, connection); } }); afterAll(async () => { const client = await getTestClient(); // close all connections if (client) { await Promise.all([ client.end(), ...[...connections.values()].map(async (connection) => { if (connection.isConnected) { await connection.close(); } }), ]); } });