Skip to content

Commit

Permalink
test(test): Setup jest and supertest for unit and e2e testing
Browse files Browse the repository at this point in the history
setup ioc mock container for test
  • Loading branch information
AllStackDev1 committed Aug 1, 2024
1 parent f6d2d8b commit bc4d764
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 3 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ build
coverage
react-email-starter
.babelrc
tests/
events/
jest.config.ts
events/
34 changes: 34 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/
import { createDefaultPreset } from 'ts-jest';
import type { Config } from 'jest';

import { compilerOptions } from './tsconfig.json';

const config: Config = {
preset: 'ts-jest',
clearMocks: true,
collectCoverage: true,
coverageProvider: 'v8',
testEnvironment: 'node',
roots: ['<rootDir>'],
setupFilesAfterEnv: ['reflect-metadata'],
modulePaths: [compilerOptions.baseUrl],
coverageDirectory: './coverage',
moduleFileExtensions: ['js', 'ts'],
testPathIgnorePatterns: ['/node_modules/'],
coveragePathIgnorePatterns: [
'node_modules',
'src/db/migrations',
'src/db/seeders',
'src/db/config.js',
'src/tests',
],
transform: {
...createDefaultPreset().transform,
},
};

export default config;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"db:create": "pnpm run build && NODE_ENV=development npx sequelize-cli db:create",
"db:migrate:up": "pnpm run build && NODE_ENV=development npx sequelize-cli db:migrate",
"db:migrate:undo": "pnpm run build && NODE_ENV=development npx sequelize-cli db:migrate:undo",
"test": "env NODE_ENV=test jest",
"test:watch": "env NODE_ENV=test jest --runInBand --watch ./src/tests/server.test.ts",
"lint": "eslint --ignore-path .eslintignore --ext .js,.ts"
},
"author": "Chinedu",
Expand Down
5 changes: 5 additions & 0 deletions src/tests/configs/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test', () => {
test('should first', () => {
expect(1 + 2).toEqual(3);
});
});
5 changes: 5 additions & 0 deletions src/tests/controllers/auth.controller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test', () => {
test('should first', () => {
expect(1 + 2).toEqual(3);
});
});
5 changes: 5 additions & 0 deletions src/tests/controllers/user.controller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test', () => {
test('should first', () => {
expect(1 + 2).toEqual(3);
});
});
5 changes: 5 additions & 0 deletions src/tests/decorators/validate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test', () => {
test('should first', () => {
expect(1 + 2).toEqual(3);
});
});
5 changes: 5 additions & 0 deletions src/tests/models/user.model.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test', () => {
test('should first', () => {
expect(1 + 2).toEqual(3);
});
});
5 changes: 5 additions & 0 deletions src/tests/repositories/auth.repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test', () => {
test('should first', () => {
expect(1 + 2).toEqual(3);
});
});
5 changes: 5 additions & 0 deletions src/tests/repositories/user.repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test', () => {
test('should first', () => {
expect(1 + 2).toEqual(3);
});
});
40 changes: 40 additions & 0 deletions src/tests/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import request from 'supertest';
import { Express } from 'express';
import { NOT_FOUND, OK } from 'http-status';

import { TestContext } from './test.context';

import { App } from 'app';
import { AuthController, UserController } from 'controllers';

describe('Server Start', () => {
let app: Express;
beforeAll(async () => {
const testContext = new TestContext();
testContext.mock<AuthController>(() => ({}), AuthController);
testContext.mock<UserController>(() => ({}), UserController);
const _app = testContext.get(App);
await _app.initialize();
app = _app.app;
});

it('responds with a not found message', async () => {
const response = await request(app)
.get('/not-found')
.set('Accept', 'application/json')
.expect(NOT_FOUND);

expect(response.body.message).toEqual(
"Can't find /not-found on this server",
);
});

it('responds with a health status', async () => {
const response = await request(app)
.get('/health-check')
.set('Accept', 'application/json')
.expect(OK);

expect(response.body).toEqual({ status: 'success', health: '100%' });
});
});
5 changes: 5 additions & 0 deletions src/tests/services/auth.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test', () => {
test('should first', () => {
expect(1 + 2).toEqual(3);
});
});
5 changes: 5 additions & 0 deletions src/tests/services/user.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test', () => {
test('should first', () => {
expect(1 + 2).toEqual(3);
});
});
15 changes: 15 additions & 0 deletions src/tests/test.container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { interfaces } from 'inversify';

import { Container } from 'di/container';

export class TestContainer extends Container {
public rebind<T>(
serviceIdentifier: interfaces.ServiceIdentifier<T>,
): interfaces.BindingToSyntax<T> {
return this.container.rebind<T>(serviceIdentifier);
}

public get<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): T {
return this.container.get<T>(serviceIdentifier);
}
}
25 changes: 25 additions & 0 deletions src/tests/test.context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'reflect-metadata';
import { interfaces } from 'inversify';

import { TestContainer } from './test.container';

export class TestContext {
private container = new TestContainer();

public mock<T>(
implementation: () => Partial<T>,
serviceIdentifier: interfaces.ServiceIdentifier<T>,
): T {
const mock = this.mockClass<T>(implementation);
this.container.rebind<T>(serviceIdentifier).toConstantValue(mock);
return mock;
}

public get<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): T {
return this.container.get<T>(serviceIdentifier);
}

private mockClass<T>(implementation: () => Partial<T>): T {
return jest.fn(implementation)() as T;
}
}

0 comments on commit bc4d764

Please sign in to comment.