[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

feat(reporter): support `includeConsoleOutput` and `addFileAttribute` in junit (#5659)

authored by

Hiroshi Ogawa and committed by
GitHub
(May 3, 2024, 10:15 AM +0200) 2f913222 c571276a

+100 -5
+7
test/reporters/fixtures/console-simple.test.ts
··· 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 + })
+3
test/reporters/fixtures/ok.test.ts
··· 1 + import { test } from "vitest" 2 + 3 + test("ok", () => {});
+18
test/reporters/tests/junit.test.ts
··· 102 102 function stabilizeReport(report: string) { 103 103 return report.replaceAll(/(timestamp|hostname|time)=".*?"/g, '$1="..."') 104 104 } 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 + })
+52
test/reporters/tests/__snapshots__/junit.test.ts.snap
··· 1 1 // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html 2 2 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 + 3 25 exports[`emits <failure> when beforeAll/afterAll failed 1`] = ` 4 26 "<?xml version="1.0" encoding="UTF-8" ?> 5 27 <testsuites name="vitest tests" tests="8" failures="2" errors="0" time="..."> ··· 109 131 ❯ throwSuite error.test.ts:48:9 110 132 ❯ error.test.ts:4:3 111 133 </failure> 134 + </testcase> 135 + </testsuite> 136 + </testsuites> 137 + " 138 + `; 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> 112 164 </testcase> 113 165 </testsuite> 114 166 </testsuites>
+20 -5
packages/vitest/src/node/reporters/junit.ts
··· 15 15 outputFile?: string 16 16 classname?: string 17 17 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 18 28 } 19 29 20 30 function flattenTasks(task: Task, baseName = ''): Task[] { ··· 88 98 private options: JUnitOptions 89 99 90 100 constructor(options: JUnitOptions) { 91 - this.options = options 101 + this.options = { ...options } 102 + this.options.includeConsoleOutput ??= true 92 103 } 93 104 94 105 async onInit(ctx: Vitest): Promise<void> { ··· 160 171 await this.writeElement('testcase', { 161 172 // TODO: v2.0.0 Remove env variable in favor of custom reporter options, e.g. "reporters: [['json', { classname: 'something' }]]" 162 173 classname: this.options.classname ?? process.env.VITEST_JUNIT_CLASSNAME ?? filename, 174 + file: this.options.addFileAttribute ? filename : undefined, 163 175 name: task.name, 164 176 time: getDuration(task), 165 177 }, 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 + } 168 182 169 183 if (task.mode === 'skip' || task.mode === 'todo') 170 184 await this.logger.log('<skipped/>') ··· 259 273 260 274 await this.writeElement('testsuites', stats, async () => { 261 275 for (const file of transformed) { 276 + const filename = relative(this.ctx.config.root, file.filepath) 262 277 await this.writeElement('testsuite', { 263 - name: relative(this.ctx.config.root, file.filepath), 278 + name: filename, 264 279 timestamp: (new Date()).toISOString(), 265 280 hostname: hostname(), 266 281 tests: file.tasks.length, ··· 269 284 skipped: file.stats.skipped, 270 285 time: getDuration(file), 271 286 }, async () => { 272 - await this.writeTasks(file.tasks, file.name) 287 + await this.writeTasks(file.tasks, filename) 273 288 }) 274 289 } 275 290 })