Skip to content

Commit 3c85187

Browse files
authored
fix(coverage): exclude vite virtual modules by default (#3794)
1 parent e8bc46b commit 3c85187

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

docs/config/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,9 @@ List of files included in coverage as glob patterns
723723
'dist/**',
724724
'packages/*/test?(s)/**',
725725
'**/*.d.ts',
726+
'**/virtual:*',
727+
'**/__x00__*',
728+
'**/\x00*',
726729
'cypress/**',
727730
'test?(s)/**',
728731
'test?(-*).?(c|m)[jt]s?(x)',

packages/vitest/src/defaults.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const defaultCoverageExcludes = [
1515
'dist/**',
1616
'packages/*/test?(s)/**',
1717
'**/*.d.ts',
18+
'**/virtual:*',
19+
'**/__x00__*',
20+
'**/\x00*',
1821
'cypress/**',
1922
'test?(s)/**',
2023
'test?(-*).?(c|m)[jt]s?(x)',

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,19 @@ test('coverage provider does not conflict with built-in reporter\'s outputFile',
101101

102102
expect(files).toContain('junit.xml')
103103
})
104+
105+
test('virtual files should be excluded', () => {
106+
const files = fs.readdirSync(resolve('./coverage'))
107+
const srcFiles = fs.readdirSync(resolve('./coverage/src'))
108+
109+
for (const file of [...files, ...srcFiles]) {
110+
expect(file).not.toContain('virtual:')
111+
112+
// Vitest in node
113+
expect(file).not.toContain('__x00__')
114+
expect(file).not.toContain('\0')
115+
116+
// Vitest browser
117+
expect(file).not.toContain('\x00')
118+
}
119+
})

test/coverage-test/test/coverage.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import { expect, test } from 'vitest'
2+
3+
// @ts-expect-error -- untyped virtual file provided by custom plugin
4+
import virtualFile1 from 'virtual:vitest-custom-virtual-file-1'
5+
26
import { implicitElse } from '../src/implicitElse'
37
import { useImportEnv } from '../src/importEnv'
48
import { second } from '../src/function-count'
59
import { runDynamicFileCJS, runDynamicFileESM } from '../src/dynamic-files'
610

11+
// @ts-expect-error -- untyped virtual file provided by custom plugin
12+
import virtualFile2 from '\0vitest-custom-virtual-file-2'
13+
714
// Browser mode crashes with dynamic files. Enable this when browser mode works.
815
// To keep istanbul report consistent between browser and node, skip dynamic tests when istanbul is used.
916
const skipDynamicFiles = globalThis.process?.env.COVERAGE_PROVIDER === 'istanbul' || !globalThis.process?.env.COVERAGE_PROVIDER
@@ -40,3 +47,8 @@ test.skipIf(skipDynamicFiles)('run dynamic ESM file', async () => {
4047
test.skipIf(skipDynamicFiles)('run dynamic CJS file', async () => {
4148
await runDynamicFileCJS()
4249
})
50+
51+
test('virtual file imports', () => {
52+
expect(virtualFile1).toBe('This file should be excluded from coverage report #1')
53+
expect(virtualFile2).toBe('This file should be excluded from coverage report #2')
54+
})

test/coverage-test/vitest.config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ const provider = process.argv[1 + process.argv.indexOf('--provider')]
77
export default defineConfig({
88
plugins: [
99
vue(),
10+
{
11+
// Simulates Vite's virtual files: https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention
12+
name: 'vitest-custom-virtual-files',
13+
resolveId(id) {
14+
if (id === 'virtual:vitest-custom-virtual-file-1')
15+
return 'src/virtual:vitest-custom-virtual-file-1.ts'
16+
17+
if (id === '\0vitest-custom-virtual-file-2')
18+
return 'src/\0vitest-custom-virtual-file-2.ts'
19+
},
20+
load(id) {
21+
if (id === 'src/virtual:vitest-custom-virtual-file-1.ts') {
22+
return `
23+
const virtualFile = "This file should be excluded from coverage report #1"
24+
export default virtualFile;
25+
`
26+
}
27+
28+
// Vitest browser resolves this as "\x00", Node as "__x00__"
29+
if (id === 'src/__x00__vitest-custom-virtual-file-2.ts' || id === 'src/\x00vitest-custom-virtual-file-2.ts') {
30+
return `
31+
const virtualFile = "This file should be excluded from coverage report #2"
32+
export default virtualFile;
33+
`
34+
}
35+
},
36+
},
1037
],
1138
define: {
1239
MY_CONSTANT: '"my constant"',

0 commit comments

Comments
 (0)