Files
photo-live/services/api/test/health.e2e-spec.ts
T
2026-09-17 19:14:41 +08:00

48 lines
1.6 KiB
TypeScript

// services/api/test/health.e2e-spec.ts
// e2e 冒烟:验证 e2e 工装(Nest testing module + supertest + 根 .env 加载)可用。
// 不监听端口:app.init() 后直接对 app.getHttpServer() 发请求。
// 全局前缀与 main-cn.ts 保持一致('v1'),由 spec 显式设置。
import { type INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import request from 'supertest';
import { AppCnModule } from '../src/app-cn.module';
import { resetDb } from './reset';
describe('GET /v1/health (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
await resetDb();
const moduleRef = await Test.createTestingModule({
imports: [AppCnModule],
}).compile();
app = moduleRef.createNestApplication();
app.setGlobalPrefix('v1');
await app.init();
});
afterAll(async () => {
await app.close();
});
it('responds 200 {"status":"ok"}', async () => {
const res = await request(app.getHttpServer()).get('/v1/health');
expect(res.status).toBe(200);
expect(res.body).toEqual({ status: 'ok' });
});
it('sets x-request-id response header (generated when absent)', async () => {
const res = await request(app.getHttpServer()).get('/v1/health');
const requestId = res.headers['x-request-id'];
expect(typeof requestId).toBe('string');
expect(requestId.length ?? 0).toBeGreaterThan(0);
});
it('echoes incoming x-request-id header', async () => {
const res = await request(app.getHttpServer())
.get('/v1/health')
.set('x-request-id', 'e2e-fixed-request-id');
expect(res.headers['x-request-id']).toBe('e2e-fixed-request-id');
});
});