Wire API health endpoint

This commit is contained in:
dahyeon committed 2026-07-02 13:56:01 -07:00
1 parent 4115646d66
commit 34ae20476f
10 files changed
+104 -21

No files matched your search

+43
View File
@@ -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');
});
});
+9
View File
@@ -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);
}
+3 -3
View File
@@ -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' });
});
});
});
+4 -4
View File
@@ -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();
}
}
+6 -2
View File
@@ -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' };
}
}
+7
View File
@@ -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);
}
+12
View File
@@ -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 -6
View File
@@ -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();
+3 -2
View File
@@ -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 {}