Skip to content

Commit

Permalink
fix: report file error as a <failure> on JUnit (#3997)
Browse files Browse the repository at this point in the history
  • Loading branch information
3c1u authored Aug 23, 2023
1 parent 18ae07a commit 00c432f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ coverage
docs/.vitepress/cache/deps/*.*
test/core/src/self
test/workspaces/results.json
test/reporters/fixtures/with-syntax-error.test.js
17 changes: 17 additions & 0 deletions packages/vitest/src/node/reporters/junit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ export class JUnitReporter implements Reporter {
skipped: 0,
})

// If there are no tests, but the file failed to load, we still want to report it as a failure
if (tasks.length === 0 && file.result?.state === 'fail') {
stats.failures = 1

tasks.push({
id: file.id,
type: 'test',
name: file.name,
mode: 'run',
result: file.result,
meta: {},
// NOTE: not used in JUnitReporter
context: null as any,
suite: null as any,
} satisfies Task)
}

return {
...file,
tasks,
Expand Down
4 changes: 4 additions & 0 deletions test/reporters/fixtures/with-syntax-error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// NOTE: This file is intentionally not valid JavaScript.

it('should fail', () => {
)
17 changes: 17 additions & 0 deletions test/reporters/tests/junit.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { Suite, Task, TaskResult } from 'vitest'
import { expect, test } from 'vitest'
import { resolve } from 'pathe'
import { runVitest } from '../../test-utils'
import { getDuration } from '../../../packages/vitest/src/node/reporters/junit'

const root = resolve(__dirname, '../fixtures')

test('calc the duration used by junit', () => {
const result: TaskResult = { state: 'pass', duration: 0 }
const suite: Suite = {
Expand Down Expand Up @@ -34,3 +38,16 @@ test('calc the duration used by junit', () => {
result.duration = 12001
expect(getDuration(task)).toBe('12.001')
})

test('emits <failure> if a test has a syntax error', async () => {
const { stdout } = await runVitest({ reporters: 'junit', root }, ['with-syntax-error'])

let xml = stdout

// clear timestamp and hostname
xml = xml.replace(/timestamp="[^"]+"/, 'timestamp="TIMESTAMP"')
xml = xml.replace(/hostname="[^"]+"/, 'hostname="HOSTNAME"')

expect(xml).toContain('<testsuite name="with-syntax-error.test.js" timestamp="TIMESTAMP" hostname="HOSTNAME" tests="1" failures="1" errors="0" skipped="0" time="0">')
expect(xml).toContain('<failure')
})

0 comments on commit 00c432f

Please sign in to comment.