Skip to content

Commit ddbba39

Browse files
authoredMay 10, 2023
fix(reporter): prevent deleting test reports stored in coverage directory (#3331)
1 parent 5bf7eb6 commit ddbba39

File tree

8 files changed

+45
-10
lines changed

8 files changed

+45
-10
lines changed
 

‎packages/vitest/src/node/core.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,14 @@ export class Vitest {
285285
return
286286
}
287287

288-
await this.report('onInit', this)
289-
290-
await this.initCoverageProvider()
291-
await this.coverageProvider?.clean(this.config.coverage.clean)
292-
await this.initBrowserProviders()
288+
try {
289+
await this.initCoverageProvider()
290+
await this.coverageProvider?.clean(this.config.coverage.clean)
291+
await this.initBrowserProviders()
292+
}
293+
finally {
294+
await this.report('onInit', this)
295+
}
293296

294297
const files = await this.filterTestsBySource(
295298
await this.globTestFiles(filters),

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

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export class JUnitReporter implements Reporter {
7575
private baseLog!: (text: string) => Promise<void>
7676
private logger!: IndentedLogger<Promise<void>>
7777
private _timeStart = new Date()
78+
private fileFd?: fs.FileHandle
7879

7980
async onInit(ctx: Vitest): Promise<void> {
8081
this.ctx = ctx
@@ -89,6 +90,7 @@ export class JUnitReporter implements Reporter {
8990
await fs.mkdir(outputDirectory, { recursive: true })
9091

9192
const fileFd = await fs.open(this.reportFile, 'w+')
93+
this.fileFd = fileFd
9294

9395
this.baseLog = async (text: string) => await fs.writeFile(fileFd, `${text}\n`)
9496
}
@@ -244,5 +246,7 @@ export class JUnitReporter implements Reporter {
244246

245247
if (this.reportFile)
246248
this.ctx.logger.log(`JUNIT report written to ${this.reportFile}`)
249+
250+
await this.fileFd?.close()
247251
}
248252
}

‎test/config/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@vitest/test-config",
33
"private": true,
44
"scripts": {
5-
"test": "vitest run test/**"
5+
"test": "vitest run"
66
},
77
"devDependencies": {
88
"execa": "^7.0.0",

‎test/config/test/failures.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, test } from 'vitest'
2+
import { version } from 'vitest/package.json'
23

34
import { runVitest } from './utils'
45

@@ -57,3 +58,16 @@ test('boolean browser flag without dot notation, with more dot notation options'
5758
expect(error).toMatch('Error: A boolean argument "--browser" was used with dot notation arguments "--browser.name".')
5859
expect(error).toMatch('Please specify the "--browser" argument with dot notation as well: "--browser.enabled"')
5960
})
61+
62+
test('version number is printed when coverage provider fails to load', async () => {
63+
const { error, output } = await runVitest('run', [
64+
'--coverage.enabled',
65+
'--coverage.provider',
66+
'custom',
67+
'--coverage.customProviderModule',
68+
'./non-existing-module.ts',
69+
])
70+
71+
expect(output).toMatch(`RUN v${version}`)
72+
expect(error).toMatch('Error: Failed to load custom CoverageProviderModule from ./non-existing-module.ts')
73+
})

‎test/config/test/utils.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import stripAnsi from 'strip-ansi'
44
export async function runVitest(mode: 'run' | 'watch', cliArguments: string[]) {
55
const subprocess = execa('vitest', [mode, 'fixtures/test/', ...cliArguments])
66
let error = ''
7+
let output = ''
8+
9+
subprocess.stdout?.on('data', (data) => {
10+
output += stripAnsi(data.toString())
11+
})
712

813
subprocess.stderr?.on('data', (data) => {
914
error += stripAnsi(data.toString())
10-
11-
// Sometimes on Windows CI execa doesn't exit properly. Force exit when stderr is caught.
12-
subprocess.kill()
1315
})
1416

1517
await new Promise(resolve => subprocess.on('exit', resolve))
1618

17-
return { error }
19+
return { output, error }
1820
}

‎test/config/vitest.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineConfig } from 'vitest/config'
22

33
export default defineConfig({
44
test: {
5+
include: ['test/**.test.ts'],
56
testTimeout: 60_000,
67
chaiConfig: {
78
truncateThreshold: 999,

‎test/coverage-test/coverage-report-tests/generic.report.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,10 @@ test('function count is correct', async () => {
9494
expect(functions.total).toBe(5)
9595
expect(functions.covered).toBe(3)
9696
})
97+
98+
test('coverage provider does not conflict with built-in reporter\'s outputFile', async () => {
99+
const coveragePath = resolve('./coverage')
100+
const files = fs.readdirSync(coveragePath)
101+
102+
expect(files).toContain('junit.xml')
103+
})

‎test/coverage-test/testing.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const configs = [
1717
].filter(Boolean),
1818
coverage: { enabled: true },
1919
browser: { enabled: isBrowser, name: 'chrome', headless: true },
20+
21+
// Regression vitest#3330
22+
reporters: ['default', 'junit'],
23+
outputFile: { junit: 'coverage/junit.xml' },
2024
}],
2125

2226
// Run tests for checking coverage report contents.

0 commit comments

Comments
 (0)
Please sign in to comment.