diff --git a/docs/config/index.md b/docs/config/index.md index f4fbdf439b00..01a5efe5f5ad 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -1351,7 +1351,8 @@ export default defineConfig({ ``` ::: tip NOTE -Vitest automatically adds test files `include` patterns to the default value of `coverage.exclude`. +Vitest automatically adds test files `include` patterns to the `coverage.exclude`. +It's not possible to show coverage of test files. ::: #### coverage.all diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 0e18d944d49c..8d3499cd012e 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -85,6 +85,10 @@ This function is not used internally and exposed exclusively as a public API. 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`. +### Coverage ignores test files even when `coverage.excludes` is overwritten. + +It is no longer possible to include test files in coverage report by overwriting `coverage.excludes`. Test files are now always excluded. + ## Migrating to Vitest 2.0 {#vitest-2} ### Default Pool is `forks` diff --git a/packages/vitest/src/node/config/resolveConfig.ts b/packages/vitest/src/node/config/resolveConfig.ts index f55d9d3ecbe3..4255f9a12c35 100644 --- a/packages/vitest/src/node/config/resolveConfig.ts +++ b/packages/vitest/src/node/config/resolveConfig.ts @@ -352,6 +352,7 @@ export function resolveConfig( )}`, ), ) + resolved.coverage.exclude.push(...resolved.include) resolved.forceRerunTriggers = [ ...resolved.forceRerunTriggers, diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 66119740c772..1bc182c542b5 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -7,7 +7,7 @@ import { } from '@vitest/utils' import { relative } from 'pathe' import { defaultPort } from '../../constants' -import { configDefaults, coverageConfigDefaults } from '../../defaults' +import { configDefaults } from '../../defaults' import { generateScopedClassName } from '../../integrations/css/css-modules' import { resolveApiServerConfig } from '../config/resolveConfig' import { Vitest } from '../core' @@ -154,13 +154,6 @@ export async function VitestPlugin( ) config.customLogger = silenceImportViteIgnoreWarning(config.customLogger) - // 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 diff --git a/test/coverage-test/test/include-exclude.test.ts b/test/coverage-test/test/include-exclude.test.ts index d008b03c11fa..838eba466e95 100644 --- a/test/coverage-test/test/include-exclude.test.ts +++ b/test/coverage-test/test/include-exclude.test.ts @@ -16,7 +16,7 @@ test('default exclude should ignore test files', async () => { expect(coverageMap.files()).toMatchInlineSnapshot(`[]`) }) -test('overridden exclude should not apply defaults', async () => { +test('overridden exclude should still apply defaults', async () => { await runVitest({ include: ['fixtures/test/math.test.ts'], coverage: { @@ -28,11 +28,7 @@ test('overridden exclude should not apply defaults', async () => { }) const coverageMap = await readCoverageMap() - expect(coverageMap.files()).toMatchInlineSnapshot(` - [ - "/fixtures/test/math.test.ts", - ] - `) + expect(coverageMap.files()).toMatchInlineSnapshot(`[]`) }) 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 () => { 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 () => { +test('test files are automatically excluded from report when excludes is set', async () => { await runVitest({ include: ['fixtures/src/test-that-looks-like-source-file.ts'], coverage: { @@ -61,5 +57,5 @@ test('test files are not automatically excluded from report when excludes is set const coverageMap = await readCoverageMap() const files = coverageMap.files() - expect(files).toContain('/fixtures/src/test-that-looks-like-source-file.ts') + expect(files.find(file => file.includes('test-that-looks-like-source-file'))).toBeFalsy() })