feat: Adjust global hooks to work with both knex and objection
This commit is contained in:
5
packages/backend/src/types/global.d.ts
vendored
5
packages/backend/src/types/global.d.ts
vendored
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
});
|
||||
|
Reference in New Issue
Block a user