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(vitest): show beforeAll/afterAll errors in junit reporter #4819

Merged
merged 1 commit into from
Dec 31, 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
10 changes: 10 additions & 0 deletions packages/vitest/src/node/reporters/junit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { dirname, relative, resolve } from 'pathe'

import type { Task } from '@vitest/runner'
import type { ErrorWithDiff } from '@vitest/utils'
import { getSuites } from '@vitest/runner/utils'
import type { Vitest } from '../../node'
import type { Reporter } from '../../types/reporter'
import { parseErrorStacktrace } from '../../utils/source-map'
Expand Down Expand Up @@ -219,6 +220,15 @@ export class JUnitReporter implements Reporter {
skipped: 0,
})

// inject failed suites to surface errors during beforeAll/afterAll
const suites = getSuites(file)
for (const suite of suites) {
if (suite.result?.errors) {
tasks.push(suite)
stats.failures += 1
}
}

// 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
Expand Down
26 changes: 26 additions & 0 deletions test/reporters/fixtures/suite-hook-failure/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
afterAll,
beforeAll,
describe,
it,
} from 'vitest';

describe('suite with beforeAll', () => {
beforeAll(() => {
throw new Error('beforeAll error');
});

it('ok 1', () => {});
it('ok 2', () => {});
it.skip('skip 1', () => {});
});

describe('suite with afterAll', () => {
afterAll(() => {
throw new Error('afterAll error');
});

it('ok 1', () => {});
it('ok 2', () => {});
it.skip('skip 1', () => {});
});
3 changes: 3 additions & 0 deletions test/reporters/fixtures/suite-hook-failure/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
36 changes: 36 additions & 0 deletions test/reporters/tests/__snapshots__/junit.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`emits <failure> when beforeAll/afterAll failed 1`] = `
"<?xml version="1.0" encoding="UTF-8" ?>
<testsuites name="vitest tests" tests="8" failures="2" errors="0" time="...">
<testsuite name="basic.test.ts" timestamp="..." hostname="..." tests="8" failures="2" errors="0" skipped="2" time="...">
<testcase classname="basic.test.ts" name="suite with beforeAll &gt; ok 1" time="...">
</testcase>
<testcase classname="basic.test.ts" name="suite with beforeAll &gt; ok 2" time="...">
</testcase>
<testcase classname="basic.test.ts" name="suite with beforeAll &gt; skip 1" time="...">
<skipped/>
</testcase>
<testcase classname="basic.test.ts" name="suite with afterAll &gt; ok 1" time="...">
</testcase>
<testcase classname="basic.test.ts" name="suite with afterAll &gt; ok 2" time="...">
</testcase>
<testcase classname="basic.test.ts" name="suite with afterAll &gt; skip 1" time="...">
<skipped/>
</testcase>
<testcase classname="basic.test.ts" name="suite with beforeAll" time="...">
<failure message="beforeAll error" type="Error">
Error: beforeAll error
❯ basic.test.ts:10:11
</failure>
</testcase>
<testcase classname="basic.test.ts" name="suite with afterAll" time="...">
<failure message="afterAll error" type="Error">
Error: afterAll error
❯ basic.test.ts:20:11
</failure>
</testcase>
</testsuite>
</testsuites>
"
`;
7 changes: 7 additions & 0 deletions test/reporters/tests/junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ test('emits <failure> if a test has a syntax error', async () => {
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')
})

test('emits <failure> when beforeAll/afterAll failed', async () => {
let { stdout } = await runVitest({ reporters: 'junit', root: './fixtures/suite-hook-failure' })
// reduct non-deterministic output
stdout = stdout.replaceAll(/(timestamp|hostname|time)=".*?"/g, '$1="..."')
expect(stdout).toMatchSnapshot()
})
Loading