Skip to content

Commit

Permalink
fix(coverage)!: always exclude test files (#7254)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Jan 15, 2025
1 parent 1c2b210 commit b526896
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ export function resolveConfig(
)}`,
),
)
resolved.coverage.exclude.push(...resolved.include)

resolved.forceRerunTriggers = [
...resolved.forceRerunTriggers,
Expand Down
9 changes: 1 addition & 8 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions test/coverage-test/test/include-exclude.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -28,11 +28,7 @@ test('overridden exclude should not apply defaults', async () => {
})

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

test('test file is excluded from report when excludes is not set', async () => {
Expand All @@ -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: {
Expand All @@ -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('<process-cwd>/fixtures/src/test-that-looks-like-source-file.ts')
expect(files.find(file => file.includes('test-that-looks-like-source-file'))).toBeFalsy()
})

0 comments on commit b526896

Please sign in to comment.