Skip to content

Commit 2f91322

Browse files
authoredMay 3, 2024··
feat(reporter): support includeConsoleOutput and addFileAttribute in junit (#5659)
1 parent c571276 commit 2f91322

File tree

5 files changed

+100
-5
lines changed

5 files changed

+100
-5
lines changed
 

‎packages/vitest/src/node/reporters/junit.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ export interface JUnitOptions {
1515
outputFile?: string
1616
classname?: string
1717
suiteName?: string
18+
/**
19+
* Write <system-out> and <system-err> for console output
20+
* @default true
21+
*/
22+
includeConsoleOutput?: boolean
23+
/**
24+
* Add <testcase file="..."> attribute (validated on CIRCLE CI and GitLab CI)
25+
* @default false
26+
*/
27+
addFileAttribute?: boolean
1828
}
1929

2030
function flattenTasks(task: Task, baseName = ''): Task[] {
@@ -88,7 +98,8 @@ export class JUnitReporter implements Reporter {
8898
private options: JUnitOptions
8999

90100
constructor(options: JUnitOptions) {
91-
this.options = options
101+
this.options = { ...options }
102+
this.options.includeConsoleOutput ??= true
92103
}
93104

94105
async onInit(ctx: Vitest): Promise<void> {
@@ -160,11 +171,14 @@ export class JUnitReporter implements Reporter {
160171
await this.writeElement('testcase', {
161172
// TODO: v2.0.0 Remove env variable in favor of custom reporter options, e.g. "reporters: [['json', { classname: 'something' }]]"
162173
classname: this.options.classname ?? process.env.VITEST_JUNIT_CLASSNAME ?? filename,
174+
file: this.options.addFileAttribute ? filename : undefined,
163175
name: task.name,
164176
time: getDuration(task),
165177
}, async () => {
166-
await this.writeLogs(task, 'out')
167-
await this.writeLogs(task, 'err')
178+
if (this.options.includeConsoleOutput) {
179+
await this.writeLogs(task, 'out')
180+
await this.writeLogs(task, 'err')
181+
}
168182

169183
if (task.mode === 'skip' || task.mode === 'todo')
170184
await this.logger.log('<skipped/>')
@@ -259,8 +273,9 @@ export class JUnitReporter implements Reporter {
259273

260274
await this.writeElement('testsuites', stats, async () => {
261275
for (const file of transformed) {
276+
const filename = relative(this.ctx.config.root, file.filepath)
262277
await this.writeElement('testsuite', {
263-
name: relative(this.ctx.config.root, file.filepath),
278+
name: filename,
264279
timestamp: (new Date()).toISOString(),
265280
hostname: hostname(),
266281
tests: file.tasks.length,
@@ -269,7 +284,7 @@ export class JUnitReporter implements Reporter {
269284
skipped: file.stats.skipped,
270285
time: getDuration(file),
271286
}, async () => {
272-
await this.writeTasks(file.tasks, file.name)
287+
await this.writeTasks(file.tasks, filename)
273288
})
274289
}
275290
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { describe, expect, test } from 'vitest'
2+
3+
test('test', () => {
4+
console.log('__test_stdout__')
5+
console.error('__test_stderr__')
6+
expect(0).toBe(0)
7+
})

‎test/reporters/fixtures/ok.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from "vitest"
2+
3+
test("ok", () => {});

‎test/reporters/tests/__snapshots__/junit.test.ts.snap

+52
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`addFileAttribute false 1`] = `
4+
"<?xml version="1.0" encoding="UTF-8" ?>
5+
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="...">
6+
<testsuite name="ok.test.ts" timestamp="..." hostname="..." tests="1" failures="0" errors="0" skipped="0" time="...">
7+
<testcase classname="ok.test.ts" name="ok" time="...">
8+
</testcase>
9+
</testsuite>
10+
</testsuites>
11+
"
12+
`;
13+
14+
exports[`addFileAttribute true 1`] = `
15+
"<?xml version="1.0" encoding="UTF-8" ?>
16+
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="...">
17+
<testsuite name="ok.test.ts" timestamp="..." hostname="..." tests="1" failures="0" errors="0" skipped="0" time="...">
18+
<testcase classname="ok.test.ts" file="ok.test.ts" name="ok" time="...">
19+
</testcase>
20+
</testsuite>
21+
</testsuites>
22+
"
23+
`;
24+
325
exports[`emits <failure> when beforeAll/afterAll failed 1`] = `
426
"<?xml version="1.0" encoding="UTF-8" ?>
527
<testsuites name="vitest tests" tests="8" failures="2" errors="0" time="...">
@@ -114,3 +136,33 @@ Error: throwSuite
114136
</testsuites>
115137
"
116138
`;
139+
140+
exports[`includeConsoleOutput false 1`] = `
141+
"<?xml version="1.0" encoding="UTF-8" ?>
142+
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="...">
143+
<testsuite name="console-simple.test.ts" timestamp="..." hostname="..." tests="1" failures="0" errors="0" skipped="0" time="...">
144+
<testcase classname="console-simple.test.ts" name="test" time="...">
145+
</testcase>
146+
</testsuite>
147+
</testsuites>
148+
"
149+
`;
150+
151+
exports[`includeConsoleOutput true 1`] = `
152+
"<?xml version="1.0" encoding="UTF-8" ?>
153+
<testsuites name="vitest tests" tests="1" failures="0" errors="0" time="...">
154+
<testsuite name="console-simple.test.ts" timestamp="..." hostname="..." tests="1" failures="0" errors="0" skipped="0" time="...">
155+
<testcase classname="console-simple.test.ts" name="test" time="...">
156+
<system-out>
157+
__test_stdout__
158+
159+
</system-out>
160+
<system-err>
161+
__test_stderr__
162+
163+
</system-err>
164+
</testcase>
165+
</testsuite>
166+
</testsuites>
167+
"
168+
`;

‎test/reporters/tests/junit.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,21 @@ test('options.suiteName changes name property', async () => {
102102
function stabilizeReport(report: string) {
103103
return report.replaceAll(/(timestamp|hostname|time)=".*?"/g, '$1="..."')
104104
}
105+
106+
test.each([true, false])('includeConsoleOutput %s', async (t) => {
107+
const { stdout } = await runVitest({
108+
reporters: [['junit', { includeConsoleOutput: t }]],
109+
root,
110+
include: ['console-simple.test.ts'],
111+
})
112+
expect(stabilizeReport(stdout)).matchSnapshot()
113+
})
114+
115+
test.each([true, false])('addFileAttribute %s', async (t) => {
116+
const { stdout } = await runVitest({
117+
reporters: [['junit', { addFileAttribute: t }]],
118+
root,
119+
include: ['ok.test.ts'],
120+
})
121+
expect(stabilizeReport(stdout)).matchSnapshot()
122+
})

0 commit comments

Comments
 (0)
Please sign in to comment.