feat: test ping
Some checks failed
CI / Lint, Typecheck, Test & Build (push) Has been cancelled

Created a health check endpoint with NestJS module structure. Added jest dependency to package.json to fix test runner issue. Implemented HealthModule, HealthController, and HealthService with GET /health endpoint returning status, timestamp, and uptime. Added comprehensive unit tests for both controller and service components.

Generated by AI Feature Factory
This commit is contained in:
AI Feature Factory
2026-03-03 19:01:44 +00:00
parent f29a923e6e
commit 558716a886
7 changed files with 184 additions and 20 deletions

11
src/app.module.ts Normal file
View File

@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { HealthModule } from './health/health.module';
@Module({
imports: [HealthModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

View File

@@ -0,0 +1,37 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HealthController } from './health.controller';
import { HealthService } from './health.service';
describe('HealthController', () => {
let controller: HealthController;
let service: HealthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HealthController],
providers: [HealthService],
}).compile();
controller = module.get<HealthController>(HealthController);
service = module.get<HealthService>(HealthService);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
describe('getHealth', () => {
it('should return health status', () => {
const result = {
status: 'OK',
timestamp: '2023-01-01T00:00:00.000Z',
uptime: 123.456,
};
jest.spyOn(service, 'getHealthStatus').mockReturnValue(result);
expect(controller.getHealth()).toBe(result);
expect(service.getHealthStatus).toHaveBeenCalled();
});
});
});

View File

@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { HealthService } from './health.service';
@Controller('health')
export class HealthController {
constructor(private readonly healthService: HealthService) {}
@Get()
getHealth() {
return this.healthService.getHealthStatus();
}
}

View File

@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { HealthController } from './health.controller';
import { HealthService } from './health.service';
@Module({
controllers: [HealthController],
providers: [HealthService],
})
export class HealthModule {}

View File

@@ -0,0 +1,40 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HealthService } from './health.service';
describe('HealthService', () => {
let service: HealthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [HealthService],
}).compile();
service = module.get<HealthService>(HealthService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('getHealthStatus', () => {
it('should return health status with OK status', () => {
const result = service.getHealthStatus();
expect(result).toHaveProperty('status', 'OK');
expect(result).toHaveProperty('timestamp');
expect(result).toHaveProperty('uptime');
expect(typeof result.timestamp).toBe('string');
expect(typeof result.uptime).toBe('number');
});
it('should return current timestamp', () => {
const before = new Date();
const result = service.getHealthStatus();
const after = new Date();
const timestamp = new Date(result.timestamp);
expect(timestamp.getTime()).toBeGreaterThanOrEqual(before.getTime());
expect(timestamp.getTime()).toBeLessThanOrEqual(after.getTime());
});
});
});

View File

@@ -0,0 +1,12 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class HealthService {
getHealthStatus() {
return {
status: 'OK',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
};
}
}