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: report file error as a <failure> on JUnit #3997

Merged
merged 2 commits into from
Aug 23, 2023
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
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')
})
Loading