Skip to content

Commit

Permalink
fix(junit): fix testsuites time to be aggregate sum of all testsuite …
Browse files Browse the repository at this point in the history
…items

not the total time of execution
  • Loading branch information
saitonakamura committed Nov 29, 2024
1 parent 1fa16f9 commit 225c9a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
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
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

0 comments on commit 225c9a4

Please sign in to comment.