coverage

  • Type:
type CoverageOptions = {
  enabled?: boolean;
  provider?: 'istanbul';
  exclude?: string[];
  reporters?: (keyof ReportOptions | ReportWithOptions)[];
  reportsDirectory?: string;
  clean?: boolean;
  thresholds?: CoverageThresholds;
};
  • Default: undefined
  • Version: >=0.4.0

Collect code coverage and generate coverage reports.

$ npx rstest --coverage

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 index.ts |     100 |      100 |     100 |     100 |
----------|---------|----------|---------|---------|-------------------

Options

enabled

  • Type: boolean
  • Default: false
  • CLI: --coverage, --coverage=false

Enable or disable test coverage collection.

CLI
rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  coverage: {
    enabled: true,
  },
});

provider

  • Type: 'istanbul'
  • Default: 'istanbul'

The coverage provider to use. Currently, only istanbul is supported.

rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  coverage: {
    enabled: true,
    provider: 'istanbul',
  },
});

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.

npm
yarn
pnpm
bun
npm add @rstest/coverage-istanbul -D

@rstest/coverage-istanbul is powered by swc-plugin-coverage-instrument.

exclude

  • Type: string[]
  • Default:
[
  '**/node_modules/**',
  '**/dist/**',
  '**/test/**',
  '**/__tests__/**',
  '**/__mocks__/**',
  '**/*.{test,spec}.[jt]s',
  '**/*.{test,spec}.[c|m][jt]s',
  '**/*.{test,spec}.[jt]sx',
  '**/*.{test,spec}.[c|m][jt]sx',
];

A glob pattern array to exclude files from test coverage collection.

rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  coverage: {
    enabled: true,
    exclude: ['**/node_modules/**', '**/dist/**'],
  },
});

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.

rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  coverage: {
    enabled: true,
    reporters: [
      'html',
      ['text', { skipFull: true }],
      ['json', { file: 'coverage-final.json' }],
    ],
  },
});

reportsDirectory

  • Type: string
  • Default: './coverage'

The directory to store coverage reports.

rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  coverage: {
    enabled: true,
    reportsDirectory: './coverage-reports',
  },
});

clean

  • Type: boolean
  • Default: true

Whether to clean the coverage directory before running tests.

rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  coverage: {
    enabled: true,
    clean: true,
  },
});

thresholds

  • Type:
type CoverageThresholds = {
  /** Thresholds for statements */
  statements?: number;
  /** Thresholds for functions */
  functions?: number;
  /** Thresholds for branches */
  branches?: number;
  /** Thresholds for lines */
  lines?: number;
};
  • 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.

rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  coverage: {
    enabled: true,
    thresholds: {
      statements: 80,
      functions: 80,
      branches: 80,
      lines: -10,
    },
  },
});

When the code coverage is below the specified thresholds, the test will fail and output an error message like below:

Error: Coverage for statements 75% does not meet global threshold 80%
Error: Coverage for functions 75% does not meet global threshold 80%
Error: Coverage for branches 75% does not meet global threshold 80%
Error: Uncovered lines 20 exceeds maximum global threshold allowed 10