-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(test): Setup jest and supertest for unit and e2e testing
setup ioc mock container for test
- Loading branch information
1 parent
f6d2d8b
commit bc4d764
Showing
15 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,4 @@ build | |
coverage | ||
react-email-starter | ||
.babelrc | ||
tests/ | ||
events/ | ||
jest.config.ts | ||
events/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('test', () => { | ||
test('should first', () => { | ||
expect(1 + 2).toEqual(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('test', () => { | ||
test('should first', () => { | ||
expect(1 + 2).toEqual(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('test', () => { | ||
test('should first', () => { | ||
expect(1 + 2).toEqual(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('test', () => { | ||
test('should first', () => { | ||
expect(1 + 2).toEqual(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('test', () => { | ||
test('should first', () => { | ||
expect(1 + 2).toEqual(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('test', () => { | ||
test('should first', () => { | ||
expect(1 + 2).toEqual(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('test', () => { | ||
test('should first', () => { | ||
expect(1 + 2).toEqual(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('test', () => { | ||
test('should first', () => { | ||
expect(1 + 2).toEqual(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('test', () => { | ||
test('should first', () => { | ||
expect(1 + 2).toEqual(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |