Skip to content

Commit

Permalink
fix(coverage): support overriding exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jun 29, 2024
1 parent d7f23d0 commit 9ee4549
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
9 changes: 2 additions & 7 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import type {
} from 'vitest'
import {
coverageConfigDefaults,
defaultExclude,
defaultInclude,
} from 'vitest/config'
import { BaseCoverageProvider } from 'vitest/coverage'
import c from 'picocolors'
Expand Down Expand Up @@ -120,11 +118,8 @@ export class IstanbulCoverageProvider

this.testExclude = new _TestExclude({
cwd: ctx.config.root,
include:
typeof this.options.include === 'undefined'
? undefined
: [...this.options.include],
exclude: [...defaultExclude, ...defaultInclude, ...this.options.exclude],
include: this.options.include,
exclude: this.options.exclude,
excludeNodeModules: true,
extension: this.options.extension,
relativePath: !this.options.allowExternal,
Expand Down
9 changes: 2 additions & 7 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import { cleanUrl } from 'vite-node/utils'
import type { EncodedSourceMap, FetchResult } from 'vite-node'
import {
coverageConfigDefaults,
defaultExclude,
defaultInclude,
} from 'vitest/config'
import { BaseCoverageProvider } from 'vitest/coverage'
import type {
Expand Down Expand Up @@ -126,11 +124,8 @@ export class V8CoverageProvider

this.testExclude = new _TestExclude({
cwd: ctx.config.root,
include:
typeof this.options.include === 'undefined'
? undefined
: [...this.options.include],
exclude: [...defaultExclude, ...defaultInclude, ...this.options.exclude],
include: this.options.include,
exclude: this.options.exclude,
excludeNodeModules: true,
extension: this.options.extension,
relativePath: !this.options.allowExternal,
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const defaultCoverageExcludes = [
'**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*',
'**/vitest.{workspace,projects}.[jt]s?(on)',
'**/.{eslint,mocha,prettier}rc.{?(c|m)js,yml}',
...defaultExclude,
...defaultInclude,
]

// These are the generic defaults for coverage. Providers may also set some provider specific defaults.
Expand Down
39 changes: 39 additions & 0 deletions test/coverage-test/test/include-exclude.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect } from 'vitest'
import { coverageTest, normalizeURL, readCoverageMap, runVitest, test } from '../utils'

test('default exclude should ignore test files', async () => {
await runVitest({
include: [normalizeURL(import.meta.url)],
coverage: {
all: true,
reporter: 'json',
include: ['fixtures/test/math.test.ts'],
},
})

const coverageMap = await readCoverageMap()
expect(coverageMap.files()).toMatchInlineSnapshot(`[]`)
})

test('overriden exclude should not apply defaults', async () => {
await runVitest({
include: [normalizeURL(import.meta.url)],
coverage: {
all: true,
reporter: 'json',
include: ['fixtures/test/math.test.ts'],
exclude: ['dont-match-anything'],
},
})

const coverageMap = await readCoverageMap()
expect(coverageMap.files()).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/test/math.test.ts",
]
`)
})

coverageTest('dummy', () => {
expect(1 + 1).toBe(2)
})

0 comments on commit 9ee4549

Please sign in to comment.