Skip to content

Commit b526896

Browse files
authoredJan 15, 2025··
fix(coverage)!: always exclude test files (#7254)
1 parent 1c2b210 commit b526896

File tree

5 files changed

+12
-17
lines changed

5 files changed

+12
-17
lines changed
 

‎docs/config/index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,8 @@ export default defineConfig({
13511351
```
13521352

13531353
::: tip NOTE
1354-
Vitest automatically adds test files `include` patterns to the default value of `coverage.exclude`.
1354+
Vitest automatically adds test files `include` patterns to the `coverage.exclude`.
1355+
It's not possible to show coverage of test files.
13551356
:::
13561357

13571358
#### coverage.all

‎docs/guide/migration.md

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ This function is not used internally and exposed exclusively as a public API.
8585

8686
The `vitest/reporters` entrypoint now only exports reporters implementations and options types. If you need access to `TestCase`/`TestSuite` and other task related types, import them additionally from `vitest/node`.
8787

88+
### Coverage ignores test files even when `coverage.excludes` is overwritten.
89+
90+
It is no longer possible to include test files in coverage report by overwriting `coverage.excludes`. Test files are now always excluded.
91+
8892
## Migrating to Vitest 2.0 {#vitest-2}
8993

9094
### Default Pool is `forks`

‎packages/vitest/src/node/config/resolveConfig.ts

+1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ export function resolveConfig(
352352
)}`,
353353
),
354354
)
355+
resolved.coverage.exclude.push(...resolved.include)
355356

356357
resolved.forceRerunTriggers = [
357358
...resolved.forceRerunTriggers,

‎packages/vitest/src/node/plugins/index.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@vitest/utils'
88
import { relative } from 'pathe'
99
import { defaultPort } from '../../constants'
10-
import { configDefaults, coverageConfigDefaults } from '../../defaults'
10+
import { configDefaults } from '../../defaults'
1111
import { generateScopedClassName } from '../../integrations/css/css-modules'
1212
import { resolveApiServerConfig } from '../config/resolveConfig'
1313
import { Vitest } from '../core'
@@ -154,13 +154,6 @@ export async function VitestPlugin(
154154
)
155155
config.customLogger = silenceImportViteIgnoreWarning(config.customLogger)
156156

157-
// If "coverage.exclude" is not defined by user, add "test.include" to "coverage.exclude" automatically
158-
if (userConfig.coverage?.enabled && !userConfig.coverage.exclude && userConfig.include && config.test) {
159-
config.test.coverage = {
160-
exclude: [...coverageConfigDefaults.exclude, ...userConfig.include],
161-
}
162-
}
163-
164157
// we want inline dependencies to be resolved by analyser plugin so module graph is populated correctly
165158
if (viteConfig.ssr?.noExternal !== true) {
166159
const inline = testConfig.server?.deps?.inline

‎test/coverage-test/test/include-exclude.test.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test('default exclude should ignore test files', async () => {
1616
expect(coverageMap.files()).toMatchInlineSnapshot(`[]`)
1717
})
1818

19-
test('overridden exclude should not apply defaults', async () => {
19+
test('overridden exclude should still apply defaults', async () => {
2020
await runVitest({
2121
include: ['fixtures/test/math.test.ts'],
2222
coverage: {
@@ -28,11 +28,7 @@ test('overridden exclude should not apply defaults', async () => {
2828
})
2929

3030
const coverageMap = await readCoverageMap()
31-
expect(coverageMap.files()).toMatchInlineSnapshot(`
32-
[
33-
"<process-cwd>/fixtures/test/math.test.ts",
34-
]
35-
`)
31+
expect(coverageMap.files()).toMatchInlineSnapshot(`[]`)
3632
})
3733

3834
test('test file is excluded from report when excludes is not set', async () => {
@@ -49,7 +45,7 @@ test('test file is excluded from report when excludes is not set', async () => {
4945
expect(files.find(file => file.includes('test-that-looks-like-source-file'))).toBeFalsy()
5046
})
5147

52-
test('test files are not automatically excluded from report when excludes is set', async () => {
48+
test('test files are automatically excluded from report when excludes is set', async () => {
5349
await runVitest({
5450
include: ['fixtures/src/test-that-looks-like-source-file.ts'],
5551
coverage: {
@@ -61,5 +57,5 @@ test('test files are not automatically excluded from report when excludes is set
6157

6258
const coverageMap = await readCoverageMap()
6359
const files = coverageMap.files()
64-
expect(files).toContain('<process-cwd>/fixtures/src/test-that-looks-like-source-file.ts')
60+
expect(files.find(file => file.includes('test-that-looks-like-source-file'))).toBeFalsy()
6561
})

0 commit comments

Comments
 (0)
Please sign in to comment.