I'm trying to do integration testing against a real database and I'm new to jest and knex..
Here's what I have so far:
describe('FooRepository', () => {
let dbName;
let conn;
beforeAll(async () => {
conn = getConnection();
dbName = cryptoRandomString({ length: 5, characters: 'abcdefghijklmnopqrstuvwxyz' });
await conn.raw(`CREATE DATABASE ${dbName}`);
Model.knex(conn);
});
afterAll(async () => {
await conn.raw(`DROP DATABASE ${dbName}`);
await conn.destroy();
});
beforeEach(async () => {
await conn.migrate.latest();
});
afterEach(async () => {
await conn.migrate.rollback();
});
describe('getFooByID', () => {
// test getFooByID()
})
})
How can I put this dbName, conn, and those before/after actions into somewhere reusable? Thanks