Merge pull request #1312 from automatisch/global-test-hooks

feat: Add global hooks for jest
This commit is contained in:
Ömer Faruk Aydın
2023-10-04 11:45:37 +02:00
committed by GitHub
8 changed files with 57 additions and 14 deletions

View File

@@ -2,4 +2,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
setupFilesAfterEnv: ['./test/setup/global-hooks.ts'],
globalTeardown: './test/setup/global-teardown.ts',
};

View File

@@ -1,6 +1,6 @@
import appConfig from './src/config/app';
const fileExtension = appConfig.isDev ? 'ts' : 'js';
const fileExtension = appConfig.isDev || appConfig.isTest ? 'ts' : 'js';
const knexConfig = {
client: 'pg',

View File

@@ -17,6 +17,7 @@ type AppConfig = {
appEnv: string;
logLevel: string;
isDev: boolean;
isTest: boolean;
isProd: boolean;
postgresDatabase: string;
postgresSchema: string;
@@ -89,6 +90,7 @@ const appConfig: AppConfig = {
appEnv: appEnv,
logLevel: process.env.LOG_LEVEL || 'info',
isDev: appEnv === 'development',
isTest: appEnv === 'test',
isProd: appEnv === 'production',
version: process.env.npm_package_version,
postgresDatabase: process.env.POSTGRES_DATABASE || 'automatisch_development',

View File

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

View File

@@ -1,11 +0,0 @@
import { createDatabaseAndUser } from '../../bin/database/utils';
import logger from '../../src/helpers/logger';
createDatabaseAndUser()
.then(() => {
process.exit(0);
})
.catch((error) => {
logger.error(error);
process.exit(1);
});

View File

@@ -0,0 +1,17 @@
import { client as knex } from '../../src/config/database';
global.beforeAll(async () => {
global.knex = knex;
});
global.beforeEach(async function () {
this.transaction = await global.knex.transaction();
});
global.afterEach(async function () {
await this.transaction.rollback();
});
global.afterAll(async () => {
global.knex.destroy();
});

View File

@@ -0,0 +1,5 @@
const exitProcess = (): void => {
process.exit(0);
};
export default exitProcess;

View File

@@ -1,2 +1,22 @@
import './check-env-file';
import './create-database';
import { createDatabaseAndUser } from '../../bin/database/utils';
import { client as knex } from '../../src/config/database';
import logger from '../../src/helpers/logger';
const createAndMigrateDatabase = async () => {
await createDatabaseAndUser();
const migrator = knex.migrate;
await migrator.latest();
logger.info(`Completed database migrations for the test database.`);
};
createAndMigrateDatabase()
.then(() => {
process.exit(0);
})
.catch((error) => {
logger.error(error);
process.exit(1);
});