feat: 增强测试稳定性和可靠性

- 增加登录重试机制(最多3次),提高登录成功率
- 添加后端健康监控,每30秒检查一次后端状态
- 改进测试隔离,每个测试后清理localStorage和sessionStorage
- 优化错误处理和日志输出
- 添加globalTeardown函数,确保测试后正确清理资源
This commit is contained in:
张翔
2026-04-04 13:29:06 +08:00
parent be1c587dbf
commit aedca1cf85
3 changed files with 123 additions and 28 deletions
+71
View File
@@ -8,6 +8,42 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let backendProcess: ChildProcess | null = null;
let healthCheckInterval: NodeJS.Timeout | null = null;
async function checkBackendHealth(): Promise<boolean> {
try {
const response = await fetch('http://localhost:8084/actuator/health', {
signal: AbortSignal.timeout(5000)
} as any);
if (response.ok) {
const data = await response.json();
return data.status === 'UP';
}
return false;
} catch (error) {
return false;
}
}
function startHealthMonitoring() {
if (healthCheckInterval) {
clearInterval(healthCheckInterval);
}
healthCheckInterval = setInterval(async () => {
const isHealthy = await checkBackendHealth();
if (!isHealthy) {
console.error('⚠️ 后端服务健康检查失败!');
}
}, 30000);
}
function stopHealthMonitoring() {
if (healthCheckInterval) {
clearInterval(healthCheckInterval);
healthCheckInterval = null;
}
}
async function globalSetup(config: FullConfig) {
console.log('🚀 开始全局测试环境设置...');
@@ -84,6 +120,8 @@ async function globalSetup(config: FullConfig) {
console.log('🧹 清理测试数据...');
await cleanupTestData();
startHealthMonitoring();
console.log('✅ 全局测试环境设置完成');
}
@@ -198,4 +236,37 @@ async function cleanupTestData(): Promise<void> {
}
}
async function globalTeardown() {
console.log('🧹 开始全局测试环境清理...');
stopHealthMonitoring();
if (backendProcess) {
console.log('🛑 停止后端服务...');
backendProcess.kill('SIGTERM');
await new Promise<void>((resolve) => {
if (backendProcess) {
backendProcess.on('exit', () => {
console.log('✅ 后端服务已停止');
resolve();
});
setTimeout(() => {
if (backendProcess) {
backendProcess.kill('SIGKILL');
console.log('⚠️ 强制停止后端服务');
resolve();
}
}, 10000);
} else {
resolve();
}
});
}
console.log('✅ 全局测试环境清理完成');
}
export default globalSetup;
export { globalTeardown };