Skip to content

Commit

Permalink
fix(coverage): exclude vite virtual files by default
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 21, 2023
1 parent 8693449 commit 4e65526
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/config/index.md
Expand Up @@ -720,6 +720,7 @@ List of files included in coverage as glob patterns
'dist/**',
'packages/*/test?(s)/**',
'**/*.d.ts',
'**/virtual:*',
'cypress/**',
'test?(s)/**',
'test?(-*).?(c|m)[jt]s?(x)',
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/defaults.ts
Expand Up @@ -15,6 +15,7 @@ const defaultCoverageExcludes = [
'dist/**',
'packages/*/test?(s)/**',
'**/*.d.ts',
'**/virtual:*',
'cypress/**',
'test?(s)/**',
'test?(-*).?(c|m)[jt]s?(x)',
Expand Down
Expand Up @@ -101,3 +101,11 @@ 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:')
})
7 changes: 7 additions & 0 deletions test/coverage-test/test/coverage.test.ts
@@ -1,4 +1,7 @@
import { expect, test } from 'vitest'

// @ts-expect-error -- untyped virtual file provided by custom plugin
import virtualFile from 'virtual:vitest-custom-virtual-file'
import { implicitElse } from '../src/implicitElse'
import { useImportEnv } from '../src/importEnv'
import { second } from '../src/function-count'
Expand Down Expand Up @@ -40,3 +43,7 @@ test.skipIf(skipDynamicFiles)('run dynamic ESM file', async () => {
test.skipIf(skipDynamicFiles)('run dynamic CJS file', async () => {
await runDynamicFileCJS()
})

test('virtual file import', () => {
expect(virtualFile).toBe('This file should be excluded from coverage report')
})
16 changes: 16 additions & 0 deletions test/coverage-test/vitest.config.ts
Expand Up @@ -7,6 +7,22 @@ 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')
return 'src/virtual:vitest-custom-virtual-file.ts'
},
load(id) {
if (id === 'src/virtual:vitest-custom-virtual-file.ts') {
return `
const virtualFile = "This file should be excluded from coverage report"
export default virtualFile;
`
}
},
},
],
define: {
MY_CONSTANT: '"my constant"',
Expand Down

0 comments on commit 4e65526

Please sign in to comment.