fix(e2e): 修复测试报告统计问题

问题:
- 测试报告显示所有统计数据为0或NaN
- calculateStats方法使用了错误的数据源

修复:
- 使用testResults数组代替result.suites
- 添加空数组检查避免除零错误
- 修复duration字段访问方式

优势:
- 测试报告正确显示统计数据
- 避免NaN错误
- 提供准确的测试执行信息
This commit is contained in:
张翔
2026-04-07 13:30:40 +08:00
parent 87c9816689
commit d52a1f1204
2 changed files with 23 additions and 8 deletions
+22 -7
View File
@@ -36,17 +36,32 @@ class CustomReporter implements Reporter {
}
private calculateStats(result: FullResult): TestStats {
const suites = result.suites || [];
const allTests = suites.flatMap(suite =>
suite.specs.flatMap(spec => spec.tests)
);
const allTests = this.testResults;
if (allTests.length === 0) {
return {
total: 0,
passed: 0,
failed: 0,
skipped: 0,
flaky: 0,
passRate: 0,
failRate: 0,
skipRate: 0,
flakyRate: 0,
totalDuration: 0,
avgDuration: 0,
slowestTests: [],
failedTests: [],
};
}
const passed = allTests.filter(t => t.status === 'passed');
const failed = allTests.filter(t => t.status === 'failed');
const skipped = allTests.filter(t => t.status === 'skipped');
const flaky = allTests.filter(t => t.status === 'passed' && t.retry >= 1);
const totalDuration = allTests.reduce((sum, t) => sum + (t.duration || 0), 0);
const totalDuration = allTests.reduce((sum, t) => sum + t.duration, 0);
const avgDuration = totalDuration / allTests.length;
const passRate = (passed.length / allTests.length) * 100;
@@ -67,8 +82,8 @@ class CustomReporter implements Reporter {
totalDuration,
avgDuration,
slowestTests: allTests
.filter(t => t.duration)
.sort((a, b) => (b.duration || 0) - (a.duration || 0))
.filter(t => t.duration > 0)
.sort((a, b) => b.duration - a.duration)
.slice(0, 10),
failedTests: failed,
};
@@ -14,7 +14,7 @@
},
{
"name": "token",
"value": "eyJhbGciOiJIUzM4NCJ9.eyJyb2xlcyI6W10sInVzZXJJZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInN1YiI6ImFkbWluIiwiaWF0IjoxNzc1NTM4MjAxLCJleHAiOjE3NzU2MjQ2MDF9.2Nnu8oTldNpyqZ-6CsgxAFrK5KpVfv0m97Zbe1uxXnShxRQkQwdSxnY3lY9xk0A4"
"value": "eyJhbGciOiJIUzM4NCJ9.eyJyb2xlcyI6W10sInVzZXJJZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInN1YiI6ImFkbWluIiwiaWF0IjoxNzc1NTM4NzgxLCJleHAiOjE3NzU2MjUxODF9.-YWMX8Fdyt8-W0rppNP5QT6t2KI2HMj95LTijW209EBAav4u1lNuZZvBLUp0mbhT"
}
]
}