Skip to content

Commit 3acc187

Browse files
committed
test: re-implement e2e test with standard naming and structure
1 parent 2f478b6 commit 3acc187

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

src/modules/app/app.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class AppConfig {
3939
const { BASE_PATH, CLUSTERING, LOG_LEVEL, NODE_ENV } = process.env;
4040

4141
return {
42+
// Exclude may not work for e2e testing
4243
exclude: [
4344
{
4445
method: RequestMethod.ALL,

src/modules/app/app.service.spec.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('AppService', () => {
1313
service = app.get<AppService>(AppService);
1414
});
1515

16-
describe('get the app version number', () => {
16+
describe('getVersion()', () => {
1717
it('should return version number', async () => {
1818
const result: VersionRes = {
1919
version: process.env.npm_package_version,
@@ -22,10 +22,9 @@ describe('AppService', () => {
2222
});
2323
});
2424

25-
describe('get the health status', () => {
26-
const OK = 'OK';
27-
it(`should return ${OK}`, async () => {
28-
expect(service.healthz()).toEqual(OK);
25+
describe('healthz()', () => {
26+
it('should return health status', async () => {
27+
expect(service.healthz()).toEqual('OK');
2928
});
3029
});
3130
});

src/utils/helper.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ApiResponseOptions } from '@nestjs/swagger';
22
import { INestApplication } from '@nestjs/common';
3+
import { NodeEnv } from '@share/enums';
34
import { NormalException } from '@/exception/normal.exception';
45

56
/**
@@ -19,10 +20,13 @@ export const toSwaggerError = (
1920
* Encapsulate the init setup for bootstrap, E2E testing and swagger script resued
2021
*/
2122
export const initialize = (app: INestApplication) => {
23+
const { BASE_PATH, NODE_ENV } = process.env;
24+
2225
app.enableVersioning();
2326

24-
// Enable CORS by default because Swagger UI required
25-
app.enableCors();
27+
// For Swagger UI
28+
if (NODE_ENV === NodeEnv.DEVELOPMENT) app.enableCors();
2629

27-
app.setGlobalPrefix(process.env.BASE_PATH);
30+
// For convenience
31+
if (BASE_PATH && NODE_ENV !== NodeEnv.TEST) app.setGlobalPrefix(BASE_PATH);
2832
};

test/app.e2e-spec.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import {
33
FastifyAdapter,
44
NestFastifyApplication,
55
} from '@nestjs/platform-fastify';
6+
import { HttpSuccessResponse } from '@share/interfaces';
67
import { Test, TestingModule } from '@nestjs/testing';
8+
import { des } from './common';
79
import { initialize } from '@util/helper';
8-
import { wrapDataObj } from './common';
910

10-
describe('AppController (e2e)', () => {
11+
describe('AppModule', () => {
1112
let app: NestFastifyApplication;
1213

13-
const { BASE_PATH, npm_package_version } = process.env;
14+
const { npm_package_version } = process.env;
1415

1516
beforeAll(async () => {
1617
const moduleFixture: TestingModule = await Test.createTestingModule({
@@ -27,24 +28,26 @@ describe('AppController (e2e)', () => {
2728
await app.getHttpAdapter().getInstance().ready();
2829
});
2930

30-
const versionPath = `${BASE_PATH}/version`;
31-
it(`${versionPath} (GET)`, async () => {
32-
const result: VersionRes = { version: npm_package_version };
31+
des({ url: '/version' }, async (config) => {
32+
it('should return version number with 200 status code', async () => {
33+
const expectedResult: VersionRes = { version: npm_package_version };
34+
const response = await app.inject(config);
35+
const actualResult = response.json<HttpSuccessResponse<VersionRes>>();
3336

34-
const response = await app.inject({
35-
url: versionPath,
37+
expect(response.statusCode).toEqual(200);
38+
expect(actualResult.data).toEqual(expectedResult);
3639
});
37-
expect(response.statusCode).toEqual(200);
38-
expect(response.json()).toEqual(wrapDataObj(result));
3940
});
4041

41-
const healthPath = `${BASE_PATH}/healthz`;
42-
it(`${healthPath} (GET)`, async () => {
43-
const response = await app.inject({
44-
url: healthPath,
42+
des({ url: '/healthz' }, async (config) => {
43+
it('should return health status with 200 status code', async () => {
44+
const expectedResult = 'OK';
45+
const response = await app.inject(config);
46+
const actualResult = response.json<HttpSuccessResponse<string>>();
47+
48+
expect(response.statusCode).toEqual(200);
49+
expect(actualResult.data).toEqual(expectedResult);
4550
});
46-
expect(response.statusCode).toEqual(200);
47-
expect(response.json()).toEqual(wrapDataObj('OK'));
4851
});
4952

5053
afterAll(async () => {

test/common.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import { InjectOptions } from 'fastify';
2+
13
/**
2-
* Returns an object with a 'data' key.
4+
* Wrapper function for showing request URL and method in the description
35
*/
4-
export const wrapDataObj = <T = any>(data: T) => {
5-
return { data };
6+
export const des = (
7+
config: InjectOptions,
8+
action: (config: InjectOptions) => Promise<void> | void
9+
) => {
10+
describe(`${config.url} (${config.method || 'GET'})`, () => {
11+
action(config);
12+
});
613
};

0 commit comments

Comments
 (0)