Wire API health endpoint
This commit is contained in:
1 parent
4115646d66
commit
34ae20476f
10 files changed
+104
-21
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { bootstrap } from './app.bootstrap';
|
||||
import { configureApp } from './app.setup';
|
||||
|
||||
type ListenFunction = (port: string | number) => Promise<void>;
|
||||
|
||||
jest.mock('@nestjs/core', () => ({
|
||||
NestFactory: {
|
||||
create: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('./app.setup', () => ({
|
||||
API_GLOBAL_PREFIX: 'api',
|
||||
configureApp: jest.fn(),
|
||||
}));
|
||||
|
||||
describe('bootstrap', () => {
|
||||
const originalPort = process.env.PORT;
|
||||
|
||||
afterEach(() => {
|
||||
process.env.PORT = originalPort;
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('applies shared app setup before listening', async () => {
|
||||
process.env.PORT = '3123';
|
||||
|
||||
const mockListen = jest
|
||||
.fn<ReturnType<ListenFunction>, Parameters<ListenFunction>>()
|
||||
.mockResolvedValue(undefined);
|
||||
const mockApp = { listen: mockListen };
|
||||
const mockedNestFactory = NestFactory as unknown as {
|
||||
create: jest.MockedFunction<() => Promise<typeof mockApp>>;
|
||||
};
|
||||
mockedNestFactory.create.mockResolvedValue(mockApp);
|
||||
|
||||
await bootstrap();
|
||||
|
||||
expect(configureApp).toHaveBeenCalledWith(mockApp);
|
||||
expect(mockListen).toHaveBeenCalledWith('3123');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { configureApp } from './app.setup';
|
||||
|
||||
export async function bootstrap(): Promise<void> {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
configureApp(app);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
@@ -14,9 +14,9 @@ describe('AppController', () => {
|
||||
appController = app.get<AppController>(AppController);
|
||||
});
|
||||
|
||||
describe('root', () => {
|
||||
it('should return "Hello World!"', () => {
|
||||
expect(appController.getHello()).toBe('Hello World!');
|
||||
describe('health', () => {
|
||||
it('returns the API health status', () => {
|
||||
expect(appController.getHealth()).toEqual({ status: 'ok' });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,12 +1,12 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
import { AppService, type HealthResponse } from './app.service';
|
||||
|
||||
@Controller()
|
||||
@Controller('health')
|
||||
export class AppController {
|
||||
constructor(private readonly appService: AppService) {}
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
return this.appService.getHello();
|
||||
getHealth(): HealthResponse {
|
||||
return this.appService.getHealth();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
export interface HealthResponse {
|
||||
status: 'ok';
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
getHealth(): HealthResponse {
|
||||
return { status: 'ok' };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
|
||||
export const API_GLOBAL_PREFIX = 'api';
|
||||
|
||||
export function configureApp(app: INestApplication): void {
|
||||
app.setGlobalPrefix(API_GLOBAL_PREFIX);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { bootstrap } from './app.bootstrap';
|
||||
import './main';
|
||||
|
||||
jest.mock('./app.bootstrap', () => ({
|
||||
bootstrap: jest.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
describe('main', () => {
|
||||
it('starts the Nest application bootstrap', () => {
|
||||
expect(bootstrap).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -1,8 +1,3 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { bootstrap } from './app.bootstrap';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
void bootstrap();
|
||||
@@ -1,11 +1,12 @@
|
||||
import { Module, Global } from '@nestjs/common';
|
||||
import { DatabaseModule } from './database/database.module';
|
||||
import { RedisModule } from './redis/redis.module';
|
||||
import { SecretModule } from './secret/secret.module';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [SecretModule, DatabaseModule],
|
||||
imports: [SecretModule, DatabaseModule, RedisModule],
|
||||
providers: [],
|
||||
exports: [SecretModule, DatabaseModule],
|
||||
exports: [SecretModule, DatabaseModule, RedisModule],
|
||||
})
|
||||
export class GlobalModule {}
|
||||
@@ -2,7 +2,10 @@ import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { App } from 'supertest/types';
|
||||
import { configureApp } from './../src/app.setup';
|
||||
import { AppModule } from './../src/app.module';
|
||||
import { ConnectionService } from './../src/modules/global/database/connection.service';
|
||||
import { REDIS_CLIENT } from './../src/modules/global/redis/redis.constants';
|
||||
|
||||
describe('AppController (e2e)', () => {
|
||||
let app: INestApplication<App>;
|
||||
@@ -10,17 +13,26 @@ describe('AppController (e2e)', () => {
|
||||
beforeEach(async () => {
|
||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
}).compile();
|
||||
})
|
||||
.overrideProvider(ConnectionService)
|
||||
.useValue({})
|
||||
.overrideProvider(REDIS_CLIENT)
|
||||
.useValue({
|
||||
destroy: jest.fn(),
|
||||
quit: jest.fn().mockResolvedValue(undefined),
|
||||
})
|
||||
.compile();
|
||||
|
||||
app = moduleFixture.createNestApplication();
|
||||
configureApp(app);
|
||||
await app.init();
|
||||
});
|
||||
|
||||
it('/ (GET)', () => {
|
||||
it('/api/health (GET)', () => {
|
||||
return request(app.getHttpServer())
|
||||
.get('/')
|
||||
.get('/api/health')
|
||||
.expect(200)
|
||||
.expect('Hello World!');
|
||||
.expect({ status: 'ok' });
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
|
||||
Reference in new issue
Block a user