-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcoverage.md
More file actions
510 lines (409 loc) · 15.2 KB
/
coverage.md
File metadata and controls
510 lines (409 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
---
title: Coverage | Guide
---
# Coverage
Vitest supports Native code coverage via [`v8`](https://v8.dev/blog/javascript-code-coverage) and instrumented code coverage via [`istanbul`](https://istanbul.js.org/).
## Coverage Providers
Both `v8` and `istanbul` support are optional. By default, `v8` will be used.
You can select the coverage tool by setting `test.coverage.provider` to `v8` or `istanbul`:
```ts [vitest.config.ts]
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
coverage: {
provider: 'v8' // or 'istanbul'
},
},
})
```
When you start the Vitest process, it will prompt you to install the corresponding support package automatically.
Or if you prefer to install them manually:
::: code-group
```bash [v8]
npm i -D @vitest/coverage-v8
```
```bash [istanbul]
npm i -D @vitest/coverage-istanbul
```
:::
## V8 Provider
::: info
The description of V8 coverage below is Vitest specific and does not apply to other test runners.
Since `v3.2.0` Vitest has used [AST based coverage remapping](/blog/vitest-3-2#coverage-v8-ast-aware-remapping) for V8 coverage, which produces identical coverage reports to Istanbul.
This allows users to have the speed of V8 coverage with accuracy of Istanbul coverage.
:::
By default Vitest uses `'v8'` coverage provider.
This provider requires Javascript runtime that's implemented on top of [V8 engine](https://v8.dev/), such as NodeJS, Deno or any Chromium based browsers such as Google Chrome.
Coverage collection is performed during runtime by instructing V8 using [`node:inspector`](https://nodejs.org/api/inspector.html) and [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/tot/Profiler/) in browsers. User's source files can be executed as-is without any pre-instrumentation steps.
- ✅ Recommended option to use
- ✅ No pre-transpile step. Test files can be executed as-is.
- ✅ Faster execute times than Istanbul.
- ✅ Lower memory usage than Istanbul.
- ✅ Coverage report accuracy is as good as with Istanbul ([since Vitest `v3.2.0`](/blog/vitest-3-2#coverage-v8-ast-aware-remapping)).
- ⚠️ In some cases can be slower than Istanbul, e.g. when loading lots of different modules. V8 does not support limiting coverage collection to specific modules.
- ⚠️ There are some minor limitations set by V8 engine. See [`ast-v8-to-istanbul` | Limitations](https://github.com/AriPerkkio/ast-v8-to-istanbul?tab=readme-ov-file#limitations).
- ❌ Does not work on environments that don't use V8, such as Firefox or Bun. Or on environments that don't expose V8 coverage via profiler, such as Cloudflare Workers.
<script setup>
import ArrowDown from '../.vitepress/components/ArrowDown.vue'
import Box from '../.vitepress/components/Box.vue'
</script>
<div style="display: flex; flex-direction: column; align-items: center; padding: 2rem 0; max-width: 20rem;">
<Box>Test file</Box>
<ArrowDown />
<Box>Enable V8 runtime coverage collection</Box>
<ArrowDown />
<Box>Run file</Box>
<ArrowDown />
<Box>Collect coverage results from V8</Box>
<ArrowDown />
<Box>Remap coverage results to source files</Box>
<ArrowDown />
<Box>Coverage report</Box>
</div>
## Istanbul Provider
[Istanbul code coverage tooling](https://istanbul.js.org/) has existed since 2012 and is very well battle-tested.
This provider works on any Javascript runtime as coverage tracking is done by instrumenting user's source files.
In practice, instrumenting source files means adding additional Javascript in user's files:
```js
// Simplified example of branch and function coverage counters
const coverage = { // [!code ++]
branches: { 1: [0, 0] }, // [!code ++]
functions: { 1: 0 }, // [!code ++]
} // [!code ++]
export function getUsername(id) {
// Function coverage increased when this is invoked // [!code ++]
coverage.functions['1']++ // [!code ++]
if (id == null) {
// Branch coverage increased when this is invoked // [!code ++]
coverage.branches['1'][0]++ // [!code ++]
throw new Error('User ID is required')
}
// Implicit else coverage increased when if-statement condition not met // [!code ++]
coverage.branches['1'][1]++ // [!code ++]
return database.getUser(id)
}
globalThis.__VITEST_COVERAGE__ ||= {} // [!code ++]
globalThis.__VITEST_COVERAGE__[filename] = coverage // [!code ++]
```
- ✅ Works on any Javascript runtime
- ✅ Widely used and battle-tested for over 13 years.
- ✅ In some cases faster than V8. Coverage instrumentation can be limited to specific files, as opposed to V8 where all modules are instrumented.
- ❌ Requires pre-instrumentation step
- ❌ Execution speed is slower than V8 due to instrumentation overhead
- ❌ Instrumentation increases file sizes
- ❌ Memory usage is higher than V8
<div style="display: flex; flex-direction: column; align-items: center; padding: 2rem 0; max-width: 20rem;">
<Box>Test file</Box>
<ArrowDown />
<Box>Pre‑instrumentation with Babel</Box>
<ArrowDown />
<Box>Run file</Box>
<ArrowDown />
<Box>Collect coverage results from Javascript scope</Box>
<ArrowDown />
<Box>Remap coverage results to source files</Box>
<ArrowDown />
<Box>Coverage report</Box>
</div>
## Coverage Setup
::: tip
All coverage options are listed in [Coverage Config Reference](/config/coverage).
:::
To test with coverage enabled, you can pass the `--coverage` flag in CLI or set `coverage.enabled` in `vitest.config.ts`:
::: code-group
```json [package.json]
{
"scripts": {
"test": "vitest",
"coverage": "vitest run --coverage"
}
}
```
```ts [vitest.config.ts]
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
coverage: {
enabled: true
},
},
})
```
:::
## Including and Excluding Files from Coverage Report
You can define what files are shown in coverage report by configuring [`coverage.include`](/config/coverage#coverage-include) and [`coverage.exclude`](/config/coverage#coverage-exclude).
By default Vitest will show only files that were imported during test run.
To include uncovered files in the report, you'll need to configure [`coverage.include`](/config/coverage#coverage-include) with a pattern that will pick your source files:
::: code-group
```ts [vitest.config.ts] {6}
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
coverage: {
include: ['src/**/*.{ts,tsx}']
},
},
})
```
```sh [Covered Files]
├── src
│ ├── components
│ │ └── counter.tsx # [!code ++]
│ ├── mock-data
│ │ ├── products.json # [!code error]
│ │ └── users.json # [!code error]
│ └── utils
│ ├── formatters.ts # [!code ++]
│ ├── time.ts # [!code ++]
│ └── users.ts # [!code ++]
├── test
│ └── utils.test.ts # [!code error]
│
├── package.json # [!code error]
├── tsup.config.ts # [!code error]
└── vitest.config.ts # [!code error]
```
:::
To exclude files that are matching `coverage.include`, you can define an additional [`coverage.exclude`](/config/coverage#coverage-exclude):
::: code-group
```ts [vitest.config.ts] {7}
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
coverage: {
include: ['src/**/*.{ts,tsx}'],
exclude: ['**/utils/users.ts']
},
},
})
```
```sh [Covered Files]
├── src
│ ├── components
│ │ └── counter.tsx # [!code ++]
│ ├── mock-data
│ │ ├── products.json # [!code error]
│ │ └── users.json # [!code error]
│ └── utils
│ ├── formatters.ts # [!code ++]
│ ├── time.ts # [!code ++]
│ └── users.ts # [!code error]
├── test
│ └── utils.test.ts # [!code error]
│
├── package.json # [!code error]
├── tsup.config.ts # [!code error]
└── vitest.config.ts # [!code error]
```
:::
## Custom Coverage Reporter
You can use custom coverage reporters by passing either the name of the package or absolute path in `test.coverage.reporter`:
```ts [vitest.config.ts]
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
coverage: {
reporter: [
// Specify reporter using name of the NPM package
['@vitest/custom-coverage-reporter', { someOption: true }],
// Specify reporter using local path
'/absolute/path/to/custom-reporter.cjs',
],
},
},
})
```
Custom reporters are loaded by Istanbul and must match its reporter interface. See [built-in reporters' implementation](https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-reports/lib) for reference.
```js [custom-reporter.cjs]
const { ReportBase } = require('istanbul-lib-report')
module.exports = class CustomReporter extends ReportBase {
constructor(opts) {
super()
// Options passed from configuration are available here
this.file = opts.file
}
onStart(root, context) {
this.contentWriter = context.writer.writeFile(this.file)
this.contentWriter.println('Start of custom coverage report')
}
onEnd() {
this.contentWriter.println('End of custom coverage report')
this.contentWriter.close()
}
}
```
## Custom Coverage Provider
It's also possible to provide your custom coverage provider by passing `'custom'` in `test.coverage.provider`:
```ts [vitest.config.ts]
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
coverage: {
provider: 'custom',
customProviderModule: 'my-custom-coverage-provider'
},
},
})
```
The custom providers require a `customProviderModule` option which is a module name or path where to load the `CoverageProviderModule` from. It must export an object that implements `CoverageProviderModule` as default export:
```ts [my-custom-coverage-provider.ts]
import type {
CoverageProvider,
CoverageProviderModule,
ResolvedCoverageOptions,
Vitest
} from 'vitest'
const CustomCoverageProviderModule: CoverageProviderModule = {
getProvider(): CoverageProvider {
return new CustomCoverageProvider()
},
// Implements rest of the CoverageProviderModule ...
}
class CustomCoverageProvider implements CoverageProvider {
name = 'custom-coverage-provider'
options!: ResolvedCoverageOptions
initialize(ctx: Vitest) {
this.options = ctx.config.coverage
}
// Implements rest of the CoverageProvider ...
}
export default CustomCoverageProviderModule
```
Please refer to the type definition for more details.
## Ignoring Code
Both coverage providers have their own ways how to ignore code from coverage reports:
- [`v8`](https://github.com/AriPerkkio/ast-v8-to-istanbul?tab=readme-ov-file#ignoring-code)
- [`istanbul`](https://github.com/istanbuljs/nyc#parsing-hints-ignoring-lines)
When using TypeScript the source codes are transpiled using `esbuild`, which strips all comments from the source codes ([esbuild#516](https://github.com/evanw/esbuild/issues/516)).
Comments which are considered as [legal comments](https://esbuild.github.io/api/#legal-comments) are preserved.
You can include a `@preserve` keyword in the ignore hint.
Beware that these ignore hints may now be included in final production build as well.
::: tip
Follow https://github.com/vitest-dev/vitest/issues/2021 for updates about `@preserve` usage.
:::
```diff
-/* istanbul ignore if */
+/* istanbul ignore if -- @preserve */
if (condition) {
-/* v8 ignore if */
+/* v8 ignore if -- @preserve */
if (condition) {
```
### Examples
::: code-group
```ts [lines: start/stop]
/* istanbul ignore start -- @preserve */
if (parameter) { // [!code error]
console.log('Ignored') // [!code error]
} // [!code error]
else { // [!code error]
console.log('Ignored') // [!code error]
} // [!code error]
/* istanbul ignore stop -- @preserve */
console.log('Included')
/* v8 ignore start -- @preserve */
if (parameter) { // [!code error]
console.log('Ignored') // [!code error]
} // [!code error]
else { // [!code error]
console.log('Ignored') // [!code error]
} // [!code error]
/* v8 ignore stop -- @preserve */
console.log('Included')
```
```ts [if else]
/* v8 ignore if -- @preserve */
if (parameter) { // [!code error]
console.log('Ignored') // [!code error]
} // [!code error]
else {
console.log('Included')
}
/* v8 ignore else -- @preserve */
if (parameter) {
console.log('Included')
}
else { // [!code error]
console.log('Ignored') // [!code error]
} // [!code error]
```
```ts [next node]
/* v8 ignore next -- @preserve */
console.log('Ignored') // [!code error]
console.log('Included')
/* v8 ignore next -- @preserve */
function ignored() { // [!code error]
console.log('all') // [!code error]
// [!code error]
console.log('lines') // [!code error]
// [!code error]
console.log('are') // [!code error]
// [!code error]
console.log('ignored') // [!code error]
} // [!code error]
/* v8 ignore next -- @preserve */
class Ignored { // [!code error]
ignored() {} // [!code error]
alsoIgnored() {} // [!code error]
} // [!code error]
/* v8 ignore next -- @preserve */
condition // [!code error]
? console.log('ignored') // [!code error]
: console.log('also ignored') // [!code error]
```
```ts [try catch]
/* v8 ignore next -- @preserve */
try { // [!code error]
console.log('Ignored') // [!code error]
} // [!code error]
catch (error) { // [!code error]
console.log('Ignored') // [!code error]
} // [!code error]
try {
console.log('Included')
}
catch (error) {
/* v8 ignore next -- @preserve */
console.log('Ignored') // [!code error]
/* v8 ignore next -- @preserve */
console.log('Ignored') // [!code error]
}
// Requires rolldown-vite due to esbuild's lack of support.
// See https://vite.dev/guide/rolldown.html#how-to-try-rolldown
try {
console.log('Included')
}
catch (error) /* v8 ignore next */ { // [!code error]
console.log('Ignored') // [!code error]
} // [!code error]
```
```ts [switch case]
switch (type) {
case 1:
return 'Included'
/* v8 ignore next -- @preserve */
case 2: // [!code error]
return 'Ignored' // [!code error]
case 3:
return 'Included'
/* v8 ignore next -- @preserve */
default: // [!code error]
return 'Ignored' // [!code error]
}
```
```ts [whole file]
/* v8 ignore file -- @preserve */
export function ignored() { // [!code error]
return 'Whole file is ignored'// [!code error]
}// [!code error]
```
:::
## Coverage Performance
If code coverage generation is slow on your project, see [Profiling Test Performance | Code coverage](/guide/profiling-test-performance.html#code-coverage).
## Vitest UI
You can check your coverage report in [Vitest UI](/guide/ui) and [HTML reporter](/guide/reporters.html#html-reporter).
This is integrated with builtin coverage reporters with HTML output (`html`, `html-spa`, and `lcov` reporters). `html` reporter is enabled by default and this works out of the box. To integrate with custom reporters, you can configure [`coverage.htmlDir`](/config/coverage#coverage-htmldir).
<img alt="html coverage activation in Vitest UI" img-light src="/vitest-ui-show-coverage-light.png">
<img alt="html coverage activation in Vitest UI" img-dark src="/vitest-ui-show-coverage-dark.png">
<img alt="html coverage in Vitest UI" img-light src="/ui-coverage-1-light.png">
<img alt="html coverage in Vitest UI" img-dark src="/ui-coverage-1-dark.png">