Skip to content

Commit

Permalink
fix(coverage): exclude vite virtual modules by default (#3794)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Jul 28, 2023
1 parent e8bc46b commit 3c85187
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,9 @@ List of files included in coverage as glob patterns
'dist/**',
'packages/*/test?(s)/**',
'**/*.d.ts',
'**/virtual:*',
'**/__x00__*',
'**/\x00*',
'cypress/**',
'test?(s)/**',
'test?(-*).?(c|m)[jt]s?(x)',
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const defaultCoverageExcludes = [
'dist/**',
'packages/*/test?(s)/**',
'**/*.d.ts',
'**/virtual:*',
'**/__x00__*',
'**/\x00*',
'cypress/**',
'test?(s)/**',
'test?(-*).?(c|m)[jt]s?(x)',
Expand Down
16 changes: 16 additions & 0 deletions test/coverage-test/coverage-report-tests/generic.report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,19 @@ test('coverage provider does not conflict with built-in reporter\'s outputFile',

expect(files).toContain('junit.xml')
})

test('virtual files should be excluded', () => {
const files = fs.readdirSync(resolve('./coverage'))
const srcFiles = fs.readdirSync(resolve('./coverage/src'))

for (const file of [...files, ...srcFiles]) {
expect(file).not.toContain('virtual:')

// Vitest in node
expect(file).not.toContain('__x00__')
expect(file).not.toContain('\0')

// Vitest browser
expect(file).not.toContain('\x00')
}
})
12 changes: 12 additions & 0 deletions test/coverage-test/test/coverage.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { expect, test } from 'vitest'

// @ts-expect-error -- untyped virtual file provided by custom plugin
import virtualFile1 from 'virtual:vitest-custom-virtual-file-1'

import { implicitElse } from '../src/implicitElse'
import { useImportEnv } from '../src/importEnv'
import { second } from '../src/function-count'
import { runDynamicFileCJS, runDynamicFileESM } from '../src/dynamic-files'

// @ts-expect-error -- untyped virtual file provided by custom plugin
import virtualFile2 from '\0vitest-custom-virtual-file-2'

// Browser mode crashes with dynamic files. Enable this when browser mode works.
// To keep istanbul report consistent between browser and node, skip dynamic tests when istanbul is used.
const skipDynamicFiles = globalThis.process?.env.COVERAGE_PROVIDER === 'istanbul' || !globalThis.process?.env.COVERAGE_PROVIDER
Expand Down Expand Up @@ -40,3 +47,8 @@ test.skipIf(skipDynamicFiles)('run dynamic ESM file', async () => {
test.skipIf(skipDynamicFiles)('run dynamic CJS file', async () => {
await runDynamicFileCJS()
})

test('virtual file imports', () => {
expect(virtualFile1).toBe('This file should be excluded from coverage report #1')
expect(virtualFile2).toBe('This file should be excluded from coverage report #2')
})
27 changes: 27 additions & 0 deletions test/coverage-test/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ const provider = process.argv[1 + process.argv.indexOf('--provider')]
export default defineConfig({
plugins: [
vue(),
{
// Simulates Vite's virtual files: https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention
name: 'vitest-custom-virtual-files',
resolveId(id) {
if (id === 'virtual:vitest-custom-virtual-file-1')
return 'src/virtual:vitest-custom-virtual-file-1.ts'

if (id === '\0vitest-custom-virtual-file-2')
return 'src/\0vitest-custom-virtual-file-2.ts'
},
load(id) {
if (id === 'src/virtual:vitest-custom-virtual-file-1.ts') {
return `
const virtualFile = "This file should be excluded from coverage report #1"
export default virtualFile;
`
}

// Vitest browser resolves this as "\x00", Node as "__x00__"
if (id === 'src/__x00__vitest-custom-virtual-file-2.ts' || id === 'src/\x00vitest-custom-virtual-file-2.ts') {
return `
const virtualFile = "This file should be excluded from coverage report #2"
export default virtualFile;
`
}
},
},
],
define: {
MY_CONSTANT: '"my constant"',
Expand Down

0 comments on commit 3c85187

Please sign in to comment.