Skip to content

Commit ca37a06

Browse files
authoredDec 3, 2024··
fix(junit): fix testsuites time to be sum of all testsuite items (#6985)
1 parent 3496a01 commit ca37a06

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed
 

‎packages/vitest/src/node/reporters/junit.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,19 @@ export class JUnitReporter implements Reporter {
335335
(stats, file) => {
336336
stats.tests += file.tasks.length
337337
stats.failures += file.stats.failures
338+
stats.time += file.result?.duration || 0
338339
return stats
339340
},
340341
{
341342
name: this.options.suiteName || 'vitest tests',
342343
tests: 0,
343344
failures: 0,
344345
errors: 0, // we cannot detect those
345-
time: executionTime(new Date().getTime() - this._timeStart.getTime()),
346+
time: 0,
346347
},
347348
)
348349

349-
await this.writeElement('testsuites', stats, async () => {
350+
await this.writeElement('testsuites', { ...stats, time: executionTime(stats.time) }, async () => {
350351
for (const file of transformed) {
351352
const filename = relative(this.ctx.config.root, file.filepath)
352353
await this.writeElement(

‎test/reporters/tests/__snapshots__/reporters.spec.ts.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports[`JUnit reporter 1`] = `
1616

1717
exports[`JUnit reporter with custom function classnameTemplate 1`] = `
1818
"<?xml version="1.0" encoding="UTF-8" ?>
19-
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0">
19+
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0.145992842">
2020
<testsuite name="test/core/test/basic.test.ts" timestamp="2022-01-19T10:10:01.759Z" hostname="hostname" tests="1" failures="0" errors="0" skipped="0" time="0.145992842">
2121
<testcase classname="filename:basic.test.ts - filepath:/vitest/test/core/test/basic.test.ts" name="Math.sqrt()" time="0.001442286">
2222
</testcase>
@@ -27,7 +27,7 @@ exports[`JUnit reporter with custom function classnameTemplate 1`] = `
2727

2828
exports[`JUnit reporter with custom string classname 1`] = `
2929
"<?xml version="1.0" encoding="UTF-8" ?>
30-
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0">
30+
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0.145992842">
3131
<testsuite name="test/core/test/basic.test.ts" timestamp="2022-01-19T10:10:01.759Z" hostname="hostname" tests="1" failures="0" errors="0" skipped="0" time="0.145992842">
3232
<testcase classname="my-custom-classname" name="Math.sqrt()" time="0.001442286">
3333
</testcase>
@@ -38,7 +38,7 @@ exports[`JUnit reporter with custom string classname 1`] = `
3838

3939
exports[`JUnit reporter with custom string classnameTemplate 1`] = `
4040
"<?xml version="1.0" encoding="UTF-8" ?>
41-
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0">
41+
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0.145992842">
4242
<testsuite name="test/core/test/basic.test.ts" timestamp="2022-01-19T10:10:01.759Z" hostname="hostname" tests="1" failures="0" errors="0" skipped="0" time="0.145992842">
4343
<testcase classname="filename:basic.test.ts - filepath:/vitest/test/core/test/basic.test.ts" name="Math.sqrt()" time="0.001442286">
4444
</testcase>
@@ -97,7 +97,7 @@ exports[`JUnit reporter with outputFile object in non-existing directory 2`] = `
9797
9898
exports[`JUnit reporter without classname 1`] = `
9999
"<?xml version="1.0" encoding="UTF-8" ?>
100-
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0">
100+
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0.145992842">
101101
<testsuite name="test/core/test/basic.test.ts" timestamp="2022-01-19T10:10:01.759Z" hostname="hostname" tests="1" failures="0" errors="0" skipped="0" time="0.145992842">
102102
<testcase classname="test/core/test/basic.test.ts" name="Math.sqrt()" time="0.001442286">
103103
</testcase>

‎test/reporters/tests/junit.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ test('emits <failure> when beforeAll/afterAll failed', async () => {
7272
expect(xml).toMatchSnapshot()
7373
})
7474

75+
test('time', async () => {
76+
const { stdout } = await runVitest({ reporters: 'junit', root: './fixtures/duration' })
77+
78+
const xml = stabilizeReportWOTime(stdout)
79+
80+
const fastTestRegex = /<testcase classname="basic\.test\.ts" name="fast" time="(?<floatNumber>[\d.]+)">/
81+
const fastTestTime = matchJunitTime(xml, fastTestRegex)
82+
expect(fastTestTime).toBeGreaterThan(0)
83+
84+
const slowTestRegex = /<testcase classname="basic\.test\.ts" name="slow" time="(?<floatNumber>[\d.]+)">/
85+
const slowTestTime = matchJunitTime(xml, slowTestRegex)
86+
expect(slowTestTime).toBeGreaterThan(0.2)
87+
88+
const testsuiteRegex = /<testsuite name="basic\.test\.ts" timestamp="\.\.\." hostname="\.\.\." tests="2" failures="0" errors="0" skipped="0" time="(?<floatNumber>[\d.]+)">/
89+
const testsuiteTime = matchJunitTime(xml, testsuiteRegex)
90+
expect(testsuiteTime).toBeCloseTo(fastTestTime + slowTestTime, 1)
91+
92+
const testsuitesRegex = /<testsuites name="vitest tests" tests="2" failures="0" errors="0" time="(?<floatNumber>[\d.]+)">/
93+
const testsuitesTime = matchJunitTime(xml, testsuitesRegex)
94+
expect(testsuitesTime).toBeCloseTo(testsuiteTime, 1)
95+
})
96+
7597
test('format error', async () => {
7698
const { stdout } = await runVitest({ reporters: 'junit', root }, ['error.test.ts'])
7799
expect(stabilizeReport(stdout)).toMatchSnapshot()
@@ -118,6 +140,18 @@ function stabilizeReport(report: string) {
118140
return report.replaceAll(/(timestamp|hostname|time)=".*?"/g, '$1="..."')
119141
}
120142

143+
function stabilizeReportWOTime(report: string) {
144+
return report.replaceAll(/(timestamp|hostname)=".*?"/g, '$1="..."')
145+
}
146+
147+
function matchJunitTime(xml: string, regex: RegExp) {
148+
const match = xml.match(regex)
149+
expect(match).not.toBeNull()
150+
const time = Number.parseFloat(match!.groups!.floatNumber)
151+
expect(time).toBeGreaterThanOrEqual(0)
152+
return time
153+
}
154+
121155
test.each([true, false])('includeConsoleOutput %s', async (t) => {
122156
const { stdout } = await runVitest({
123157
reporters: [['junit', { includeConsoleOutput: t }]],

0 commit comments

Comments
 (0)
Please sign in to comment.