This commit is contained in:
2026-01-02 15:14:58 +08:00
parent 2f51a0498f
commit d1f2f9089e
6 changed files with 227 additions and 79 deletions

View File

@@ -1,16 +1,24 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import request from 'supertest';
import { App } from 'supertest/types';
import { PrismaService } from '../src/database/prisma.service';
import { AppModule } from './../src/app.module';
const prismaMock = {
$queryRaw: jest.fn().mockResolvedValue([1]),
};
describe('AppController (e2e)', () => {
let app: INestApplication<App>;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
})
.overrideProvider(PrismaService)
.useValue(prismaMock)
.compile();
app = moduleFixture.createNestApplication();
await app.init();
@@ -22,4 +30,50 @@ describe('AppController (e2e)', () => {
.expect(200)
.expect('Hello World!');
});
it('/health (GET) returns ok with db up', () => {
return request(app.getHttpServer())
.get('/health')
.expect('Cache-Control', 'no-store')
.expect(200)
.expect(
({
body,
}: {
body: {
status: string;
version: string;
uptimeSecs: number;
db: string;
};
}) => {
expect(body.status).toBe('ok');
expect(body.db).toBe('ok');
expect(body.version).toBeDefined();
expect(body.uptimeSecs).toBeGreaterThanOrEqual(0);
},
);
});
it('/health (GET) returns 503 with db down', async () => {
prismaMock.$queryRaw.mockRejectedValueOnce(new Error('db down'));
await request(app.getHttpServer())
.get('/health')
.expect('Cache-Control', 'no-store')
.expect(503)
.expect(
({
body,
}: {
body: {
status: string;
db: string;
};
}) => {
expect(body.status).toBe('DOWN');
expect(body.db).toBe('DOWN');
},
);
});
});