Spaces:
Running
Running
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(); | |
}); | |
}); | |
}); |