feat: Adjust global hooks to work with both knex and objection

This commit is contained in:
Faruk AYDIN
2023-10-03 19:49:19 +02:00
parent f490632722
commit f0712bd213
2 changed files with 20 additions and 7 deletions

View File

@@ -1,8 +1,11 @@
import { Knex } from 'knex';
import { Transaction } from 'objection';
declare global {
declare namespace globalThis {
// eslint-disable-next-line no-var
var knex: Knex;
var knexInstance: Knex;
// eslint-disable-next-line no-var
var knex: Transaction;
}
}

View File

@@ -1,17 +1,27 @@
import { transaction, Model } from 'objection';
import { client as knex } from '../../src/config/database';
import logger from '../../src/helpers/logger';
global.beforeAll(async () => {
global.knex = knex;
global.knexInstance = knex;
global.knex = null;
logger.silent = true;
});
global.beforeEach(async function () {
this.transaction = await global.knex.transaction();
global.beforeEach(async () => {
// It's assigned as global.knex for the convenience even though
// it's a transaction. It's rolled back after each test.
// by assigning to knex, we can use it as knex.table('example') in tests files.
global.knex = await transaction.start(knex);
Model.knex(global.knex);
});
global.afterEach(async function () {
await this.transaction.rollback();
global.afterEach(async () => {
await global.knex.rollback();
Model.knex(knex);
});
global.afterAll(async () => {
global.knex.destroy();
global.knexInstance.destroy();
logger.silent = false;
});