Some checks failed
Deploy / deploy (push) Has been cancelled
Full-stack web application for Telegram management - Frontend: Vue 3 + Vben Admin - Backend: NestJS - Features: User management, group broadcast, statistics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
106 lines
3.5 KiB
TypeScript
106 lines
3.5 KiB
TypeScript
/**
|
|
* Playwright 全局测试清理
|
|
* 在所有测试结束后执行
|
|
*/
|
|
|
|
import { FullConfig } from '@playwright/test';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
async function globalTeardown(config: FullConfig) {
|
|
console.log('🧹 开始全局测试清理...');
|
|
|
|
try {
|
|
// 更新测试指标
|
|
const testDataDir = path.join(process.cwd(), 'test-results', 'test-data');
|
|
const metricsFile = path.join(testDataDir, 'test-metrics.json');
|
|
|
|
if (fs.existsSync(metricsFile)) {
|
|
const metrics = JSON.parse(fs.readFileSync(metricsFile, 'utf-8'));
|
|
metrics.endTime = new Date().toISOString();
|
|
metrics.duration =
|
|
new Date(metrics.endTime).getTime() -
|
|
new Date(metrics.startTime).getTime();
|
|
metrics.teardownCompleted = true;
|
|
|
|
fs.writeFileSync(metricsFile, JSON.stringify(metrics, null, 2));
|
|
console.log(`⏱️ 测试总耗时: ${(metrics.duration / 1000).toFixed(2)}秒`);
|
|
}
|
|
|
|
// 清理临时文件
|
|
const authStateFile = path.join(process.cwd(), 'tests', 'auth-state.json');
|
|
if (fs.existsSync(authStateFile)) {
|
|
fs.unlinkSync(authStateFile);
|
|
console.log('🗑️ 认证状态文件已清理');
|
|
}
|
|
|
|
// 生成测试结果摘要
|
|
const testResultsDir = path.join(process.cwd(), 'test-results');
|
|
if (fs.existsSync(testResultsDir)) {
|
|
const resultsFile = path.join(testResultsDir, 'results.json');
|
|
|
|
if (fs.existsSync(resultsFile)) {
|
|
const results = JSON.parse(fs.readFileSync(resultsFile, 'utf-8'));
|
|
|
|
console.log('📊 测试结果摘要:');
|
|
console.log(` - 总测试数: ${results.stats?.total || 0}`);
|
|
console.log(` - 通过: ${results.stats?.passed || 0}`);
|
|
console.log(` - 失败: ${results.stats?.failed || 0}`);
|
|
console.log(` - 跳过: ${results.stats?.skipped || 0}`);
|
|
|
|
// 如果有失败的测试,列出来
|
|
if (results.stats?.failed > 0) {
|
|
console.log('❌ 失败的测试:');
|
|
results.suites?.forEach((suite: any) => {
|
|
suite.specs?.forEach((spec: any) => {
|
|
spec.tests?.forEach((test: any) => {
|
|
if (
|
|
test.results?.some(
|
|
(result: any) => result.status === 'failed',
|
|
)
|
|
) {
|
|
console.log(
|
|
` - ${suite.title} > ${spec.title} > ${test.title}`,
|
|
);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
// 生成简化的测试报告
|
|
const summary = {
|
|
timestamp: new Date().toISOString(),
|
|
stats: results.stats,
|
|
duration: results.stats?.duration || 0,
|
|
environment: process.env.NODE_ENV || 'test',
|
|
browser: process.env.BROWSER || 'chromium',
|
|
};
|
|
|
|
fs.writeFileSync(
|
|
path.join(testResultsDir, 'test-summary.json'),
|
|
JSON.stringify(summary, null, 2),
|
|
);
|
|
|
|
console.log('📋 测试摘要已保存到 test-results/test-summary.json');
|
|
}
|
|
}
|
|
|
|
// 检查是否需要保留测试数据
|
|
if (process.env.KEEP_TEST_DATA !== 'true') {
|
|
// 清理测试数据(根据需要)
|
|
console.log('🧹 清理测试数据...');
|
|
|
|
// 这里可以添加清理测试时创建的数据的逻辑
|
|
// 例如:删除测试用户、测试内容等
|
|
}
|
|
|
|
console.log('✅ 全局测试清理完成');
|
|
} catch (error) {
|
|
console.error('❌ 全局测试清理失败:', error);
|
|
// 不抛出错误,避免影响测试结果
|
|
}
|
|
}
|
|
|
|
export default globalTeardown;
|