Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(junit): fix testsuites time to be sum of all testsuite items #6985

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/vitest/src/node/reporters/junit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,19 @@ export class JUnitReporter implements Reporter {
(stats, file) => {
stats.tests += file.tasks.length
stats.failures += file.stats.failures
stats.time += file.result?.duration || 0
return stats
},
{
name: this.options.suiteName || 'vitest tests',
tests: 0,
failures: 0,
errors: 0, // we cannot detect those
time: executionTime(new Date().getTime() - this._timeStart.getTime()),
time: 0,
},
)

await this.writeElement('testsuites', stats, async () => {
await this.writeElement('testsuites', { ...stats, time: executionTime(stats.time) }, async () => {
for (const file of transformed) {
const filename = relative(this.ctx.config.root, file.filepath)
await this.writeElement(
Expand Down
8 changes: 4 additions & 4 deletions test/reporters/tests/__snapshots__/reporters.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports[`JUnit reporter 1`] = `

exports[`JUnit reporter with custom function classnameTemplate 1`] = `
"<?xml version="1.0" encoding="UTF-8" ?>
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0">
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0.145992842">
<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">
<testcase classname="filename:basic.test.ts - filepath:/vitest/test/core/test/basic.test.ts" name="Math.sqrt()" time="0.001442286">
</testcase>
Expand All @@ -27,7 +27,7 @@ exports[`JUnit reporter with custom function classnameTemplate 1`] = `

exports[`JUnit reporter with custom string classname 1`] = `
"<?xml version="1.0" encoding="UTF-8" ?>
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0">
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0.145992842">
<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">
<testcase classname="my-custom-classname" name="Math.sqrt()" time="0.001442286">
</testcase>
Expand All @@ -38,7 +38,7 @@ exports[`JUnit reporter with custom string classname 1`] = `

exports[`JUnit reporter with custom string classnameTemplate 1`] = `
"<?xml version="1.0" encoding="UTF-8" ?>
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0">
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0.145992842">
<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">
<testcase classname="filename:basic.test.ts - filepath:/vitest/test/core/test/basic.test.ts" name="Math.sqrt()" time="0.001442286">
</testcase>
Expand Down Expand Up @@ -97,7 +97,7 @@ exports[`JUnit reporter with outputFile object in non-existing directory 2`] = `
exports[`JUnit reporter without classname 1`] = `
"<?xml version="1.0" encoding="UTF-8" ?>
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0">
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="0.145992842">
<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">
<testcase classname="test/core/test/basic.test.ts" name="Math.sqrt()" time="0.001442286">
</testcase>
Expand Down
34 changes: 34 additions & 0 deletions test/reporters/tests/junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ test('emits <failure> when beforeAll/afterAll failed', async () => {
expect(xml).toMatchSnapshot()
})

test('time', async () => {
const { stdout } = await runVitest({ reporters: 'junit', root: './fixtures/duration' })

const xml = stabilizeReportWOTime(stdout)

const fastTestRegex = /<testcase classname="basic\.test\.ts" name="fast" time="(?<floatNumber>[\d.]+)">/
const fastTestTime = matchJunitTime(xml, fastTestRegex)
expect(fastTestTime).toBeGreaterThan(0)

const slowTestRegex = /<testcase classname="basic\.test\.ts" name="slow" time="(?<floatNumber>[\d.]+)">/
const slowTestTime = matchJunitTime(xml, slowTestRegex)
expect(slowTestTime).toBeGreaterThan(0.2)

const testsuiteRegex = /<testsuite name="basic\.test\.ts" timestamp="\.\.\." hostname="\.\.\." tests="2" failures="0" errors="0" skipped="0" time="(?<floatNumber>[\d.]+)">/
const testsuiteTime = matchJunitTime(xml, testsuiteRegex)
expect(testsuiteTime).toBeCloseTo(fastTestTime + slowTestTime, 1)

const testsuitesRegex = /<testsuites name="vitest tests" tests="2" failures="0" errors="0" time="(?<floatNumber>[\d.]+)">/
const testsuitesTime = matchJunitTime(xml, testsuitesRegex)
expect(testsuitesTime).toBeCloseTo(testsuiteTime, 1)
})

test('format error', async () => {
const { stdout } = await runVitest({ reporters: 'junit', root }, ['error.test.ts'])
expect(stabilizeReport(stdout)).toMatchSnapshot()
Expand Down Expand Up @@ -118,6 +140,18 @@ function stabilizeReport(report: string) {
return report.replaceAll(/(timestamp|hostname|time)=".*?"/g, '$1="..."')
}

function stabilizeReportWOTime(report: string) {
return report.replaceAll(/(timestamp|hostname)=".*?"/g, '$1="..."')
}

function matchJunitTime(xml: string, regex: RegExp) {
const match = xml.match(regex)
expect(match).not.toBeNull()
const time = Number.parseFloat(match!.groups!.floatNumber)
expect(time).toBeGreaterThanOrEqual(0)
return time
}

test.each([true, false])('includeConsoleOutput %s', async (t) => {
const { stdout } = await runVitest({
reporters: [['junit', { includeConsoleOutput: t }]],
Expand Down