Skip to content

Commit

Permalink
Merge pull request #1312 from automatisch/global-test-hooks
Browse files Browse the repository at this point in the history
feat: Add global hooks for jest
  • Loading branch information
farukaydin committed Oct 4, 2023
2 parents 4d454ec + 9d92509 commit 8d90cb8
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 14 deletions.
4 changes: 3 additions & 1 deletion packages/backend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
setupFilesAfterEnv: ['./test/setup/global-hooks.ts'],
globalTeardown: './test/setup/global-teardown.ts',
};
2 changes: 1 addition & 1 deletion packages/backend/knexfile.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type AppConfig = {
appEnv: string;
logLevel: string;
isDev: boolean;
isTest: boolean;
isProd: boolean;
postgresDatabase: string;
postgresSchema: string;
Expand Down Expand Up @@ -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',
Expand Down
8 changes: 8 additions & 0 deletions packages/backend/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Knex } from 'knex';

declare global {
declare namespace globalThis {
// eslint-disable-next-line no-var
var knex: Knex;
}
}
11 changes: 0 additions & 11 deletions packages/backend/test/setup/create-database.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/backend/test/setup/global-hooks.ts
Original file line number Diff line number Diff line change
@@ -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();
});
5 changes: 5 additions & 0 deletions packages/backend/test/setup/global-teardown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const exitProcess = (): void => {
process.exit(0);
};

export default exitProcess;
22 changes: 21 additions & 1 deletion packages/backend/test/setup/prepare-test-env.ts
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 8d90cb8

Please sign in to comment.