From 34ae20476f8ad54dea964bcef610cba1e8661345 Mon Sep 17 00:00:00 2001 From: dahyeon Date: Thu, 2 Jul 2026 13:56:01 -0700 Subject: [PATCH] Wire API health endpoint --- apps/api/src/app.bootstrap.spec.ts | 43 ++++++++++++++++++++ apps/api/src/app.bootstrap.ts | 9 ++++ apps/api/src/app.controller.spec.ts | 6 +-- apps/api/src/app.controller.ts | 8 ++-- apps/api/src/app.service.ts | 8 +++- apps/api/src/app.setup.ts | 7 ++++ apps/api/src/main.spec.ts | 12 ++++++ apps/api/src/main.ts | 7 +--- apps/api/src/modules/global/global.module.ts | 5 ++- apps/api/test/app.e2e-spec.ts | 20 +++++++-- 10 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 apps/api/src/app.bootstrap.spec.ts create mode 100644 apps/api/src/app.bootstrap.ts create mode 100644 apps/api/src/app.setup.ts create mode 100644 apps/api/src/main.spec.ts diff --git a/apps/api/src/app.bootstrap.spec.ts b/apps/api/src/app.bootstrap.spec.ts new file mode 100644 index 0000000..9eeccc3 --- /dev/null +++ b/apps/api/src/app.bootstrap.spec.ts @@ -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; + +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, Parameters>() + .mockResolvedValue(undefined); + const mockApp = { listen: mockListen }; + const mockedNestFactory = NestFactory as unknown as { + create: jest.MockedFunction<() => Promise>; + }; + mockedNestFactory.create.mockResolvedValue(mockApp); + + await bootstrap(); + + expect(configureApp).toHaveBeenCalledWith(mockApp); + expect(mockListen).toHaveBeenCalledWith('3123'); + }); +}); diff --git a/apps/api/src/app.bootstrap.ts b/apps/api/src/app.bootstrap.ts new file mode 100644 index 0000000..3dde78d --- /dev/null +++ b/apps/api/src/app.bootstrap.ts @@ -0,0 +1,9 @@ +import { NestFactory } from '@nestjs/core'; +import { AppModule } from './app.module'; +import { configureApp } from './app.setup'; + +export async function bootstrap(): Promise { + const app = await NestFactory.create(AppModule); + configureApp(app); + await app.listen(process.env.PORT ?? 3000); +} diff --git a/apps/api/src/app.controller.spec.ts b/apps/api/src/app.controller.spec.ts index d22f389..613a7f3 100644 --- a/apps/api/src/app.controller.spec.ts +++ b/apps/api/src/app.controller.spec.ts @@ -14,9 +14,9 @@ describe('AppController', () => { appController = app.get(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' }); }); }); }); diff --git a/apps/api/src/app.controller.ts b/apps/api/src/app.controller.ts index cce879e..b16e71e 100644 --- a/apps/api/src/app.controller.ts +++ b/apps/api/src/app.controller.ts @@ -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(); } } diff --git a/apps/api/src/app.service.ts b/apps/api/src/app.service.ts index 927d7cc..bfbc682 100644 --- a/apps/api/src/app.service.ts +++ b/apps/api/src/app.service.ts @@ -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' }; } } diff --git a/apps/api/src/app.setup.ts b/apps/api/src/app.setup.ts new file mode 100644 index 0000000..081cbc6 --- /dev/null +++ b/apps/api/src/app.setup.ts @@ -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); +} diff --git a/apps/api/src/main.spec.ts b/apps/api/src/main.spec.ts new file mode 100644 index 0000000..8762bf5 --- /dev/null +++ b/apps/api/src/main.spec.ts @@ -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); + }); +}); diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index ff7d2ee..da3f6ad 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -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(); diff --git a/apps/api/src/modules/global/global.module.ts b/apps/api/src/modules/global/global.module.ts index 4456b0a..1a29a54 100644 --- a/apps/api/src/modules/global/global.module.ts +++ b/apps/api/src/modules/global/global.module.ts @@ -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 {} diff --git a/apps/api/test/app.e2e-spec.ts b/apps/api/test/app.e2e-spec.ts index a767839..007e73f 100644 --- a/apps/api/test/app.e2e-spec.ts +++ b/apps/api/test/app.e2e-spec.ts @@ -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; @@ -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 () => {