coverage
You can use v8, istanbul or a custom coverage solution for coverage collection.
You can provide coverage options to CLI with dot notation:
npx vitest --coverage.enabled --coverage.provider=istanbulWARNING
If you are using coverage options with dot notation, don't forget to specify --coverage.enabled. Do not provide a single --coverage option in that case.
coverage.provider
- Type:
'v8' | 'istanbul' | 'custom' - Default:
'v8' - CLI:
--coverage.provider=<provider>
Use provider to select the tool for coverage collection.
coverage.enabled
- Type:
boolean - Default:
false - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.enabled,--coverage.enabled=false
Enables coverage collection. Can be overridden using --coverage CLI option.
coverage.include
- Type:
string[] - Default: Files that were imported during test run
- Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.include=<pattern>,--coverage.include=<pattern1> --coverage.include=<pattern2>
List of files included in coverage as glob patterns. By default only files covered by tests are included.
It is recommended to pass file extensions in the pattern.
See Including and excluding files from coverage report for examples.
coverage.exclude
- Type:
string[] - Default: :
[] - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.exclude=<path>,--coverage.exclude=<path1> --coverage.exclude=<path2>
List of files excluded from coverage as glob patterns.
See Including and excluding files from coverage report for examples.
coverage.clean
- Type:
boolean - Default:
true - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.clean,--coverage.clean=false
Clean coverage results before running tests
coverage.cleanOnRerun
- Type:
boolean - Default:
true - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.cleanOnRerun,--coverage.cleanOnRerun=false
Clean coverage report on watch rerun. Set to false to preserve coverage results from previous run in watch mode.
coverage.reportsDirectory
- Type:
string - Default:
'./coverage' - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.reportsDirectory=<path>
WARNING
Vitest will delete this directory before running tests if coverage.clean is enabled (default value).
Directory to write coverage report to.
To preview the coverage report in the output of HTML reporter, this option must be set as a sub-directory of the html report directory (for example ./html/coverage).
coverage.reporter
- Type:
string | string[] | [string, {}][] - Default:
['text', 'html', 'clover', 'json'] - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.reporter=<reporter>,--coverage.reporter=<reporter1> --coverage.reporter=<reporter2>
Coverage reporters to use. See istanbul documentation for detailed list of all reporters. See @types/istanbul-reporter for details about reporter specific options.
The reporter has three different types:
- A single reporter:
{ reporter: 'html' } - Multiple reporters without options:
{ reporter: ['html', 'json'] } - A single or multiple reporters with reporter options: ts
{ reporter: [ ['lcov', { 'projectRoot': './src' }], ['json', { 'file': 'coverage.json' }], ['text'] ] }
You can also pass custom coverage reporters. See Guide - Custom Coverage Reporter for more information.
{
reporter: [
// Specify reporter using name of the NPM package
'@vitest/custom-coverage-reporter',
['@vitest/custom-coverage-reporter', { someOption: true }],
// Specify reporter using local path
'/absolute/path/to/custom-reporter.cjs',
['/absolute/path/to/custom-reporter.cjs', { someOption: true }],
]
}You can check your coverage report in Vitest UI: check Vitest UI Coverage for more details.
coverage.reportOnFailure
- Type:
boolean - Default:
false - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.reportOnFailure,--coverage.reportOnFailure=false
Generate coverage report even when tests fail.
coverage.allowExternal
- Type:
boolean - Default:
false - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.allowExternal,--coverage.allowExternal=false
Collect coverage of files outside the project root.
coverage.excludeAfterRemap
- Type:
boolean - Default:
false - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.excludeAfterRemap,--coverage.excludeAfterRemap=false
Apply exclusions again after coverage has been remapped to original sources. This is useful when your source files are transpiled and may contain source maps of non-source files.
Use this option when you are seeing files that show up in report even if they match your coverage.exclude patterns.
coverage.skipFull
- Type:
boolean - Default:
false - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.skipFull,--coverage.skipFull=false
Do not show files with 100% statement, branch, and function coverage.
coverage.thresholds
Options for coverage thresholds.
If a threshold is set to a positive number, it will be interpreted as the minimum percentage of coverage required. For example, setting the lines threshold to 90 means that 90% of lines must be covered.
If a threshold is set to a negative number, it will be treated as the maximum number of uncovered items allowed. For example, setting the lines threshold to -10 means that no more than 10 lines may be uncovered.
{
coverage: {
thresholds: {
// Requires 90% function coverage
functions: 90,
// Require that no more than 10 lines are uncovered
lines: -10,
}
}
}coverage.thresholds.lines
- Type:
number - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.thresholds.lines=<number>
Global threshold for lines.
coverage.thresholds.functions
- Type:
number - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.thresholds.functions=<number>
Global threshold for functions.
coverage.thresholds.branches
- Type:
number - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.thresholds.branches=<number>
Global threshold for branches.
coverage.thresholds.statements
- Type:
number - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.thresholds.statements=<number>
Global threshold for statements.
coverage.thresholds.perFile
- Type:
boolean - Default:
false - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.thresholds.perFile,--coverage.thresholds.perFile=false
Check thresholds per file.
coverage.thresholds.autoUpdate
- Type:
boolean | function - Default:
false - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.thresholds.autoUpdate=<boolean>
Update all threshold values lines, functions, branches and statements to configuration file when current coverage is better than the configured thresholds. This option helps to maintain thresholds when coverage is improved.
You can also pass a function for formatting the updated threshold values:
{
coverage: {
thresholds: {
// Update thresholds without decimals
autoUpdate: (newThreshold) => Math.floor(newThreshold),
// 95.85 -> 95
functions: 95,
}
}
}coverage.thresholds.100
- Type:
boolean - Default:
false - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.thresholds.100,--coverage.thresholds.100=false
Sets global thresholds to 100. Shortcut for --coverage.thresholds.lines 100 --coverage.thresholds.functions 100 --coverage.thresholds.branches 100 --coverage.thresholds.statements 100.
coverage.thresholds[glob-pattern]
- Type:
{ statements?: number functions?: number branches?: number lines?: number } - Default:
undefined - Available for providers:
'v8' | 'istanbul'
Sets thresholds for files matching the glob pattern.
NOTE
Vitest counts all files, including those covered by glob-patterns, into the global coverage thresholds. This is different from Jest behavior.
{
coverage: {
thresholds: {
// Thresholds for all files
functions: 95,
branches: 70,
// Thresholds for matching glob pattern
'src/utils/**.ts': {
statements: 95,
functions: 90,
branches: 85,
lines: 80,
},
// Files matching this pattern will only have lines thresholds set.
// Global thresholds are not inherited.
'**/math.ts': {
lines: 100,
}
}
}
}coverage.thresholds[glob-pattern].100
- Type:
boolean - Default:
false - Available for providers:
'v8' | 'istanbul'
Sets thresholds to 100 for files matching the glob pattern.
{
coverage: {
thresholds: {
// Thresholds for all files
functions: 95,
branches: 70,
// Thresholds for matching glob pattern
'src/utils/**.ts': { 100: true },
'**/math.ts': { 100: true }
}
}
}coverage.ignoreClassMethods
- Type:
string[] - Default:
[] - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.ignoreClassMethods=<method>
Set to array of class method names to ignore for coverage. See istanbul documentation for more information.
coverage.watermarks
- Type:
{
statements?: [number, number],
functions?: [number, number],
branches?: [number, number],
lines?: [number, number]
}- Default:
{
statements: [50, 80],
functions: [50, 80],
branches: [50, 80],
lines: [50, 80]
}- Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.watermarks.statements=50,80,--coverage.watermarks.branches=50,80
Watermarks for statements, lines, branches and functions. See istanbul documentation for more information.
coverage.processingConcurrency
- Type:
boolean - Default:
Math.min(20, os.availableParallelism?.() ?? os.cpus().length) - Available for providers:
'v8' | 'istanbul' - CLI:
--coverage.processingConcurrency=<number>
Concurrency limit used when processing the coverage results.
coverage.customProviderModule
- Type:
string - Available for providers:
'custom' - CLI:
--coverage.customProviderModule=<path or module name>
Specifies the module name or path for the custom coverage provider module. See Guide - Custom Coverage Provider for more information.