Skip to content

Commit 41a111c

Browse files
authored
fix!(coverage): remove .all, reworked include/exclude (#7837)
1 parent 82fcf5d commit 41a111c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+514
-490
lines changed

docs/advanced/api/vitest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ function runTestSpecifications(
308308
): Promise<TestRunResult>
309309
```
310310

311-
This method runs every test based on the received [specifications](/advanced/api/test-specification). The second argument, `allTestsRun`, is used by the coverage provider to determine if it needs to instrument coverage on _every_ file in the root (this only matters if coverage is enabled and `coverage.all` is set to `true`).
311+
This method runs every test based on the received [specifications](/advanced/api/test-specification). The second argument, `allTestsRun`, is used by the coverage provider to determine if it needs to include uncovered files in report.
312312

313313
::: warning
314314
This method doesn't trigger `onWatcherRerun`, `onWatcherStart` and `onTestsRerun` callbacks. If you are rerunning tests based on the file change, consider using [`rerunTestSpecifications`](#reruntestspecifications) instead.

docs/config/index.md

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,12 +1310,12 @@ Make sure that your files are not excluded by [`server.watch.ignored`](https://v
13101310

13111311
### coverage<NonProjectOption />
13121312

1313-
You can use [`v8`](https://v8.dev/blog/javascript-code-coverage), [`istanbul`](https://istanbul.js.org/) or [a custom coverage solution](/guide/coverage#custom-coverage-provider) for coverage collection.
1313+
You can use [`v8`](/guide/coverage.html#v8-provider), [`istanbul`](/guide/coverage.html#istanbul-provider) or [a custom coverage solution](/guide/coverage#custom-coverage-provider) for coverage collection.
13141314

13151315
You can provide coverage options to CLI with dot notation:
13161316

13171317
```sh
1318-
npx vitest --coverage.enabled --coverage.provider=istanbul --coverage.all
1318+
npx vitest --coverage.enabled --coverage.provider=istanbul
13191319
```
13201320

13211321
::: warning
@@ -1342,76 +1342,26 @@ Enables coverage collection. Can be overridden using `--coverage` CLI option.
13421342
#### coverage.include
13431343

13441344
- **Type:** `string[]`
1345-
- **Default:** `['**']`
1345+
- **Default:** Files that were imported during test run
13461346
- **Available for providers:** `'v8' | 'istanbul'`
1347-
- **CLI:** `--coverage.include=<path>`, `--coverage.include=<path1> --coverage.include=<path2>`
1347+
- **CLI:** `--coverage.include=<pattern>`, `--coverage.include=<pattern1> --coverage.include=<pattern2>`
13481348

1349-
List of files included in coverage as glob patterns
1349+
List of files included in coverage as glob patterns. By default only files covered by tests are included.
13501350

1351-
#### coverage.extension
1351+
It is recommended to pass file extensions in the pattern.
13521352

1353-
- **Type:** `string | string[]`
1354-
- **Default:** `['.js', '.cjs', '.mjs', '.ts', '.mts', '.tsx', '.jsx', '.vue', '.svelte', '.marko', '.astro']`
1355-
- **Available for providers:** `'v8' | 'istanbul'`
1356-
- **CLI:** `--coverage.extension=<extension>`, `--coverage.extension=<extension1> --coverage.extension=<extension2>`
1353+
See [Including and excluding files from coverage report](/guide/coverage.html#including-and-excluding-files-from-coverage-report) for examples.
13571354

13581355
#### coverage.exclude
13591356

13601357
- **Type:** `string[]`
1361-
- **Default:**
1362-
```js
1363-
[
1364-
'coverage/**',
1365-
'dist/**',
1366-
'**/node_modules/**',
1367-
'**/[.]**',
1368-
'packages/*/test?(s)/**',
1369-
'**/*.d.ts',
1370-
'**/virtual:*',
1371-
'**/__x00__*',
1372-
'**/\x00*',
1373-
'cypress/**',
1374-
'test?(s)/**',
1375-
'test?(-*).?(c|m)[jt]s?(x)',
1376-
'**/*{.,-}{test,spec,bench,benchmark}?(-d).?(c|m)[jt]s?(x)',
1377-
'**/__tests__/**',
1378-
'**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*',
1379-
'**/vitest.{workspace,projects}.[jt]s?(on)',
1380-
'**/.{eslint,mocha,prettier}rc.{?(c|m)js,yml}',
1381-
]
1382-
```
1358+
- **Default:** : `[]`
13831359
- **Available for providers:** `'v8' | 'istanbul'`
13841360
- **CLI:** `--coverage.exclude=<path>`, `--coverage.exclude=<path1> --coverage.exclude=<path2>`
13851361

13861362
List of files excluded from coverage as glob patterns.
13871363

1388-
This option overrides all default options. Extend the default options when adding new patterns to ignore:
1389-
1390-
```ts
1391-
import { coverageConfigDefaults, defineConfig } from 'vitest/config'
1392-
1393-
export default defineConfig({
1394-
test: {
1395-
coverage: {
1396-
exclude: ['**/custom-pattern/**', ...coverageConfigDefaults.exclude]
1397-
},
1398-
},
1399-
})
1400-
```
1401-
1402-
::: tip NOTE
1403-
Vitest automatically adds test files `include` patterns to the `coverage.exclude`.
1404-
It's not possible to show coverage of test files.
1405-
:::
1406-
1407-
#### coverage.all
1408-
1409-
- **Type:** `boolean`
1410-
- **Default:** `true`
1411-
- **Available for providers:** `'v8' | 'istanbul'`
1412-
- **CLI:** `--coverage.all`, `--coverage.all=false`
1413-
1414-
Whether to include all files, including the untested ones into report.
1364+
See [Including and excluding files from coverage report](/guide/coverage.html#including-and-excluding-files-from-coverage-report) for examples.
14151365

14161366
#### coverage.clean
14171367

docs/guide/cli-generated.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,6 @@ Specify reporters (default, blob, verbose, dot, json, tap, tap-flat, junit, hang
9898

9999
Write test results to a file when supporter reporter is also specified, use cac's dot notation for individual outputs of multiple reporters (example: `--outputFile.tap=./tap.txt`)
100100

101-
### coverage.all
102-
103-
- **CLI:** `--coverage.all`
104-
- **Config:** [coverage.all](/config/#coverage-all)
105-
106-
Whether to include all files, including the untested ones into report
107-
108101
### coverage.provider
109102

110103
- **CLI:** `--coverage.provider <name>`
@@ -133,13 +126,6 @@ Files included in coverage as glob patterns. May be specified more than once whe
133126

134127
Files to be excluded in coverage. May be specified more than once when using multiple extensions (default: Visit [`coverage.exclude`](https://vitest.dev/config/#coverage-exclude))
135128

136-
### coverage.extension
137-
138-
- **CLI:** `--coverage.extension <extension>`
139-
- **Config:** [coverage.extension](/config/#coverage-extension)
140-
141-
Extension to be included in coverage. May be specified more than once when using multiple extensions (default: `[".js", ".cjs", ".mjs", ".ts", ".mts", ".tsx", ".jsx", ".vue", ".svelte"]`)
142-
143129
### coverage.clean
144130

145131
- **CLI:** `--coverage.clean`

docs/guide/coverage.md

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { defineConfig } from 'vitest/config'
1818
export default defineConfig({
1919
test: {
2020
coverage: {
21-
provider: 'istanbul' // or 'v8'
21+
provider: 'v8' // or 'istanbul'
2222
},
2323
},
2424
})
@@ -132,14 +132,13 @@ globalThis.__VITEST_COVERAGE__[filename] = coverage // [!code ++]
132132

133133
## Coverage Setup
134134

135-
:::tip
136-
It's recommended to always define [`coverage.include`](https://vitest.dev/config/#coverage-include) in your configuration file.
137-
This helps Vitest to reduce the amount of files picked by [`coverage.all`](https://vitest.dev/config/#coverage-all).
135+
::: tip
136+
All coverage options are listed in [Coverage Config Reference](/config/#coverage).
138137
:::
139138

140-
To test with coverage enabled, you can pass the `--coverage` flag in CLI.
141-
By default, reporter `['text', 'html', 'clover', 'json']` will be used.
139+
To test with coverage enabled, you can pass the `--coverage` flag in CLI or set `coverage.enabled` in `vitest.config.ts`:
142140

141+
::: code-group
143142
```json [package.json]
144143
{
145144
"scripts": {
@@ -148,20 +147,92 @@ By default, reporter `['text', 'html', 'clover', 'json']` will be used.
148147
}
149148
}
150149
```
150+
```ts [vitest.config.ts]
151+
import { defineConfig } from 'vitest/config'
152+
153+
export default defineConfig({
154+
test: {
155+
coverage: {
156+
enabled: true
157+
},
158+
},
159+
})
160+
```
161+
:::
151162

152-
To configure it, set `test.coverage` options in your config file:
163+
## Including and excluding files from coverage report
153164

154-
```ts [vitest.config.ts]
165+
You can define what files are shown in coverage report by configuring [`coverage.include`](/config/#coverage-include) and [`coverage.exclude`](/config/#coverage-exclude).
166+
167+
By default Vitest will show only files that were imported during test run.
168+
To include uncovered files in the report, you'll need to configure [`coverage.include`](/config/#coverage-include) with a pattern that will pick your source files:
169+
170+
::: code-group
171+
```ts [vitest.config.ts] {6}
172+
import { defineConfig } from 'vitest/config'
173+
174+
export default defineConfig({
175+
test: {
176+
coverage: {
177+
include: ['src/**.{ts,tsx}']
178+
},
179+
},
180+
})
181+
```
182+
```sh [Covered Files]
183+
├── src
184+
│ ├── components
185+
│ │ └── counter.tsx # [!code ++]
186+
│ ├── mock-data
187+
│ │ ├── products.json # [!code error]
188+
│ │ └── users.json # [!code error]
189+
│ └── utils
190+
│ ├── formatters.ts # [!code ++]
191+
│ ├── time.ts # [!code ++]
192+
│ └── users.ts # [!code ++]
193+
├── test
194+
│ └── utils.test.ts # [!code error]
195+
196+
├── package.json # [!code error]
197+
├── tsup.config.ts # [!code error]
198+
└── vitest.config.ts # [!code error]
199+
```
200+
:::
201+
202+
To exclude files that are matching `coverage.include`, you can define an additional [`coverage.exclude`](/config/#coverage-exclude):
203+
204+
::: code-group
205+
```ts [vitest.config.ts] {7}
155206
import { defineConfig } from 'vitest/config'
156207

157208
export default defineConfig({
158209
test: {
159210
coverage: {
160-
reporter: ['text', 'json', 'html'],
211+
include: ['src/**.{ts,tsx}'],
212+
exclude: ['**/utils/users.ts']
161213
},
162214
},
163215
})
164216
```
217+
```sh [Covered Files]
218+
├── src
219+
│ ├── components
220+
│ │ └── counter.tsx # [!code ++]
221+
│ ├── mock-data
222+
│ │ ├── products.json # [!code error]
223+
│ │ └── users.json # [!code error]
224+
│ └── utils
225+
│ ├── formatters.ts # [!code ++]
226+
│ ├── time.ts # [!code ++]
227+
│ └── users.ts # [!code error]
228+
├── test
229+
│ └── utils.test.ts # [!code error]
230+
231+
├── package.json # [!code error]
232+
├── tsup.config.ts # [!code error]
233+
└── vitest.config.ts # [!code error]
234+
```
235+
:::
165236

166237
## Custom Coverage Reporter
167238

@@ -261,22 +332,6 @@ export default CustomCoverageProviderModule
261332

262333
Please refer to the type definition for more details.
263334

264-
## Changing the Default Coverage Folder Location
265-
266-
When running a coverage report, a `coverage` folder is created in the root directory of your project. If you want to move it to a different directory, use the `test.coverage.reportsDirectory` property in the `vitest.config.js` file.
267-
268-
```js [vitest.config.js]
269-
import { defineConfig } from 'vite'
270-
271-
export default defineConfig({
272-
test: {
273-
coverage: {
274-
reportsDirectory: './tests/unit/coverage'
275-
}
276-
}
277-
})
278-
```
279-
280335
## Ignoring Code
281336

282337
Both coverage providers have their own ways how to ignore code from coverage reports:
@@ -301,10 +356,6 @@ if (condition) {
301356
if (condition) {
302357
```
303358

304-
## Other Options
305-
306-
To see all configurable options for coverage, see the [coverage Config Reference](https://vitest.dev/config/#coverage).
307-
308359
## Coverage Performance
309360

310361
If code coverage generation is slow on your project, see [Profiling Test Performance | Code coverage](/guide/profiling-test-performance.html#code-coverage).

docs/guide/migration.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,55 @@ export default defineConfig({
2121
})
2222
```
2323

24+
### Removed options `coverage.all` and `coverage.extensions`
25+
26+
In previous versions Vitest included all uncovered files in coverage report by default.
27+
This was due to `coverage.all` defaulting to `true`, and `coverage.include` defaulting to `**`.
28+
These default values were chosen for a good reason - it is impossible for testing tools to guess where users are storing their source files.
29+
30+
This ended up having Vitest's coverage providers processing unexpected files, like minified Javascript, leading to slow/stuck coverage report generations.
31+
In Vitest v4 we have removed `coverage.all` completely and <ins>**defaulted to include only covered files in the report**</ins>.
32+
33+
When upgrading to v4 it is recommended to define `coverage.include` in your configuration, and then start applying simple `coverage.exclusion` patterns if needed.
34+
35+
```ts [vitest.config.ts]
36+
export default defineConfig({
37+
test: {
38+
coverage: {
39+
// Include covered and uncovered files matching this pattern:
40+
include: ['packages/**/src/**.{js,jsx,ts,tsx}'], // [!code ++]
41+
42+
// Exclusion is applied for the files that match include pattern above
43+
// No need to define root level *.config.ts files or node_modules, as we didn't add those in include
44+
exclude: ['**/some-pattern/**'], // [!code ++]
45+
46+
// These options are removed now
47+
all: true, // [!code --]
48+
extensions: ['js', 'ts'], // [!code --]
49+
}
50+
}
51+
})
52+
```
53+
54+
If `coverage.include` is not defined, coverage report will include only files that were loaded during test run:
55+
```ts [vitest.config.ts]
56+
export default defineConfig({
57+
test: {
58+
coverage: {
59+
// Include not set, include only files that are loaded during test run
60+
include: undefined, // [!code ++]
61+
62+
// Loaded files that match this pattern will be excluded:
63+
exclude: ['**/some-pattern/**'], // [!code ++]
64+
}
65+
}
66+
})
67+
```
68+
69+
See also new guides:
70+
- [Including and excluding files from coverage report](/guide/coverage.html#including-and-excluding-files-from-coverage-report) for examples
71+
- [Profiling Test Performance | Code coverage](/guide/profiling-test-performance.html#code-coverage) for tips about debugging coverage generation
72+
2473
## Migrating to Vitest 3.0 {#vitest-3}
2574

2675
### Test Options as a Third Argument

packages/coverage-istanbul/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"istanbul-lib-source-maps": "catalog:",
5353
"istanbul-reports": "catalog:",
5454
"magicast": "catalog:",
55-
"test-exclude": "catalog:",
5655
"tinyrainbow": "catalog:"
5756
},
5857
"devDependencies": {
@@ -62,7 +61,6 @@
6261
"@types/istanbul-lib-report": "catalog:",
6362
"@types/istanbul-lib-source-maps": "catalog:",
6463
"@types/istanbul-reports": "catalog:",
65-
"@types/test-exclude": "catalog:",
6664
"pathe": "catalog:",
6765
"vitest": "workspace:*"
6866
}

0 commit comments

Comments
 (0)