coverage
- Type:
- Default:
undefined
Collect code coverage and generate coverage reports.
Options
enabled
- Type:
boolean - Default:
false - CLI:
--coverage,--coverage=false,--no-coverage
Enable or disable test coverage collection.
provider
- Type:
'istanbul' - Default:
'istanbul'
The coverage provider to use. Currently, only istanbul is supported.
Istanbul provider
Istanbul is a widely used JavaScript code coverage tool that collects code coverage information through instrumentation.
To enable istanbul coverage, you need to install the @rstest/coverage-istanbul package first.
@rstest/coverage-istanbul is powered by swc-plugin-coverage-instrument.
include
- Type:
string[] - Default:
undefined
A list of glob patterns that should be included for coverage collection.
By default, rstest will only collect coverage for files that are tested. If you want to include untested files in the coverage report, you can use the include option to specify the files or patterns to include.
Note that you should use standard glob syntax here. For a single extension, write src/**/*.ts instead of src/**/*.{ts}. Single-value braces are treated literally by the underlying glob libraries, so src/**/*.{ts} will not match .ts files.
changed
- Type:
boolean | string - Default:
undefined - CLI:
--coverage.changed,--coverage.changed=<commit>
Collect coverage only for files changed in the current Git repository. Pass a commit or branch to collect coverage for files changed since that ref.
This option is available in both config and CLI:
- Use
coverage.changedinrstest.config.tswhen you want a persistent default, for example in CI. - Use
--coverage.changedfor one-off local or CI runs.
coverage.changed only limits the coverage report scope. It does not change which tests are executed.
That means these two commands do different things:
--changedchanges the test selection and runs tests related to changed files.--coverage.changedkeeps the normal test selection, but only reports coverage for changed source files.
Interaction with --changed
- When
--changedis used andcoverage.changedis not configured, coverage reports inherit the changed files collected by--changed. - If
--changedhitsforceRerunTriggers, Rstest reruns the full test suite. Coverage stays full in that case unlesscoverage.changedis explicitly enabled. - If you want to use
--changedfor test selection but still keep full coverage reports, setcoverage.changedtofalsein config.
Common setups:
The example above still runs the normal test suite, but the coverage report only includes files changed since origin/main.
npx rstest run --changed --coverageruns changed-related tests and, by default, also limits coverage to the same changed file set.npx rstest run --coverage.changed=HEAD~1runs the normal test suite but only reports coverage for files changed sinceHEAD~1.npx rstest run --coverage.changed=origin/mainis useful when your branch is based onmain: it still runs the normal test suite, but the coverage report only includes files changed compared withorigin/main.
exclude
- Type:
string[] - Default:
A glob pattern array to exclude files from test coverage collection.
Any patterns specified here will be merged with default values.
reporters
- Type:
(ReporterName | [ReporterName, ReporterOptions>])[] - Default:
['text', 'html', 'clover', 'json']
The reporters to use for coverage collection. Each reporter can be either a string (the reporter name) or a tuple with the reporter name and its options.
- See Istanbul Reporters for available reporters.
- See @types/istanbul-reporter for details about reporter specific options.
reportsDirectory
- Type:
string - Default:
'./coverage'
The directory to store coverage reports.
reportOnFailure
- Type:
boolean - Default:
false
Whether to report coverage and check thresholds when tests fail.
allowExternal
- Type:
boolean - Default:
false - CLI:
--coverage.allowExternal
Whether to collect coverage for source files outside the project root directory. This is useful in monorepo setups where tests import modules from sibling packages.
By default, rstest excludes files outside the project root from coverage reports, which aligns with the behavior of Jest and Vitest.
clean
- Type:
boolean - Default:
true
Whether to clean the coverage directory before running tests.
thresholds
- Type:
- Default:
undefined
Coverage thresholds for enforcing minimum coverage requirements. You can set thresholds for statements, functions, branches, and lines.
Thresholds specified as a positive number are taken to be the minimum percentage required. Thresholds specified as a negative number represent the maximum number of uncovered entities allowed.
When the code coverage is below the specified thresholds, the test will fail and output an error message like below:
glob pattern
If globs are specified, thresholds will be checked for each matched file pattern. If the file specified by path is not found, an error is returned.
Following the above configuration, rstest will fail if:
- The total code coverage of all files in
src/**is below 100%. - The total code coverage of all files in
node/**/*.jsis below 90%. - The global code coverage is below 80% for statements.
check threshold for per file
Rstest also supports checking thresholds for each matched file by setting perFile to true.