Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(coverage): support overriding exclude #5997

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ All configuration options that are not supported inside a [workspace](/guide/wor

A list of glob patterns that match your test files.

::: tip NOTE
When using coverage, Vitest automatically adds test files `include` patterns to coverage's default `exclude` patterns. See [`coverage.exclude`](#coverage-exclude).
:::

### exclude

- **Type:** `string[]`
Expand Down Expand Up @@ -1111,6 +1115,7 @@ List of files included in coverage as glob patterns
[
'coverage/**',
'dist/**',
'**/node_modules/**',
'**/[.]**',
'packages/*/test?(s)/**',
'**/*.d.ts',
Expand All @@ -1120,9 +1125,9 @@ List of files included in coverage as glob patterns
'cypress/**',
'test?(s)/**',
'test?(-*).?(c|m)[jt]s?(x)',
'**/*{.,-}{test,spec}?(-d).?(c|m)[jt]s?(x)',
'**/*{.,-}{test,spec,bench,benchmark}?(-d).?(c|m)[jt]s?(x)',
'**/__tests__/**',
'**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*',
'**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*',
'**/vitest.{workspace,projects}.[jt]s?(on)',
'**/.{eslint,mocha,prettier}rc.{?(c|m)js,yml}',
]
Expand All @@ -1146,6 +1151,10 @@ export default defineConfig({
})
```

::: tip NOTE
Vitest automatically adds test files `include` patterns to the default value of `coverage.exclude`.
:::

#### coverage.all

- **Type:** `boolean`
Expand Down
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
3 changes: 2 additions & 1 deletion packages/vitest/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const benchmarkConfigDefaults: Required<
const defaultCoverageExcludes = [
'coverage/**',
'dist/**',
'**/node_modules/**',
'**/[.]**',
'packages/*/test?(s)/**',
'**/*.d.ts',
Expand All @@ -40,7 +41,7 @@ const defaultCoverageExcludes = [
'test?(-*).?(c|m)[jt]s?(x)',
'**/*{.,-}{test,spec,bench,benchmark}?(-d).?(c|m)[jt]s?(x)',
'**/__tests__/**',
'**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*',
'**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*',
'**/vitest.{workspace,projects}.[jt]s?(on)',
'**/.{eslint,mocha,prettier}rc.{?(c|m)js,yml}',
]
Expand Down
9 changes: 8 additions & 1 deletion packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UserConfig as ViteConfig, Plugin as VitePlugin } from 'vite'
import { relative } from 'pathe'
import { configDefaults } from '../../defaults'
import { configDefaults, coverageConfigDefaults } from '../../defaults'
import type { ResolvedConfig, UserConfig } from '../../types'
import {
deepMerge,
Expand Down Expand Up @@ -131,6 +131,13 @@ export async function VitestPlugin(
},
}

// If "coverage.exclude" is not defined by user, add "test.include" to "coverage.exclude" automatically
if (userConfig.coverage?.enabled && !userConfig.coverage.exclude && userConfig.include && config.test) {
config.test.coverage = {
exclude: [...coverageConfigDefaults.exclude, ...userConfig.include],
}
}

// we want inline dependencies to be resolved by analyser plugin so module graph is populated correctly
if (viteConfig.ssr?.noExternal !== true) {
const inline = testConfig.server?.deps?.inline
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test, expect } from 'vitest'
import { sum } from "./math"

test("run tests on file that looks like source file", () => {
expect(sum(1,2)).toBe(3)
})
4 changes: 2 additions & 2 deletions test/coverage-test/test/allow-external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as ExternalMath from '../../test-utils/fixtures/math'
test('{ allowExternal: true } includes files outside project root', async () => {
await runVitest({
include: [normalizeURL(import.meta.url)],
coverage: { allowExternal: true, reporter: 'json' },
coverage: { allowExternal: true, reporter: 'json', include: ['**/fixtures/**'] },
})
const coverageMap = await readCoverageMap()
const files = coverageMap.files()
Expand All @@ -21,7 +21,7 @@ test('{ allowExternal: true } includes files outside project root', async () =>
test('{ allowExternal: false } excludes files outside project root', async () => {
await runVitest({
include: [normalizeURL(import.meta.url)],
coverage: { allowExternal: false, reporter: 'json' },
coverage: { allowExternal: false, reporter: 'json', include: ['**/fixtures/**'] },
})
const coverageMap = await readCoverageMap()
const files = coverageMap.files()
Expand Down
69 changes: 69 additions & 0 deletions test/coverage-test/test/include-exclude.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { expect } from 'vitest'
import { coverageConfigDefaults } from 'vitest/config'
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",
]
`)
})

test('test file is excluded from report when excludes is not set', async () => {
await runVitest({
include: ['fixtures/src/test-that-looks-like-source-file.ts'],
coverage: {
all: true,
reporter: 'json',
},
})

const coverageMap = await readCoverageMap()
const files = coverageMap.files()
expect(files.find(file => file.includes('test-that-looks-like-source-file'))).toBeFalsy()
})

test('test files are not automatically excluded from report when excludes is set', async () => {
await runVitest({
include: ['fixtures/src/test-that-looks-like-source-file.ts'],
coverage: {
all: true,
reporter: 'json',
exclude: [...coverageConfigDefaults.exclude, '**/something-else/**'],
},
})

const coverageMap = await readCoverageMap()
const files = coverageMap.files()
expect(files).toContain('<process-cwd>/fixtures/src/test-that-looks-like-source-file.ts')
})

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