Spaces:
Running
Running
File size: 2,409 Bytes
28e1dba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import request from 'supertest';
import express from 'express';
import healthRoutes from '../routes/health.js';
// 创建测试应用
const app = express();
app.use('/api', healthRoutes);
describe('Health Routes', () => {
describe('GET /api/health', () => {
it('should return health status', async () => {
const response = await request(app)
.get('/api/health')
.expect('Content-Type', /json/);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('status');
expect(response.body).toHaveProperty('timestamp');
expect(response.body).toHaveProperty('uptime');
expect(response.body).toHaveProperty('responseTime');
expect(response.body).toHaveProperty('services');
// 检查服务状态结构
expect(response.body.services).toHaveProperty('database');
expect(response.body.services).toHaveProperty('browser');
expect(response.body.services).toHaveProperty('storage');
});
it('should include memory information', async () => {
const response = await request(app)
.get('/api/health');
expect(response.body).toHaveProperty('memory');
expect(response.body.memory).toHaveProperty('used');
expect(response.body.memory).toHaveProperty('total');
expect(response.body.memory).toHaveProperty('system');
expect(typeof response.body.memory.used).toBe('number');
expect(typeof response.body.memory.total).toBe('number');
expect(typeof response.body.memory.system).toBe('number');
});
it('should include environment information', async () => {
const response = await request(app)
.get('/api/health');
expect(response.body).toHaveProperty('version');
expect(response.body).toHaveProperty('environment');
});
});
describe('GET /api/ping', () => {
it('should return pong response', async () => {
const response = await request(app)
.get('/api/ping')
.expect('Content-Type', /json/)
.expect(200);
expect(response.body).toHaveProperty('message', 'pong');
expect(response.body).toHaveProperty('timestamp');
// 验证时间戳格式
const timestamp = new Date(response.body.timestamp);
expect(timestamp).toBeInstanceOf(Date);
expect(timestamp.getTime()).not.toBeNaN();
});
});
}); |