|
| 1 | +# coverage |
| 2 | + |
| 3 | +- **Type:** |
| 4 | + |
| 5 | +```ts |
| 6 | +type CoverageOptions = { |
| 7 | + enabled?: boolean; |
| 8 | + provider?: 'istanbul'; |
| 9 | + exclude?: string[]; |
| 10 | + reporters?: (keyof ReportOptions | ReportWithOptions)[]; |
| 11 | + reportsDirectory?: string; |
| 12 | + clean?: boolean; |
| 13 | + thresholds?: CoverageThresholds; |
| 14 | +}; |
| 15 | +``` |
| 16 | + |
| 17 | +- **Default:** `undefined` |
| 18 | +- **Version:** `>=0.4.0` |
| 19 | + |
| 20 | +Collect test coverage information and generate coverage reports. |
| 21 | + |
| 22 | +```ts title='rstest.config.ts' |
| 23 | +import { defineConfig } from '@rstest/core'; |
| 24 | + |
| 25 | +export default defineConfig({ |
| 26 | + coverage: { |
| 27 | + enabled: true, |
| 28 | + }, |
| 29 | +}); |
| 30 | +``` |
| 31 | + |
| 32 | +## Options |
| 33 | + |
| 34 | +### enabled |
| 35 | + |
| 36 | +- **Type:** `boolean` |
| 37 | +- **Default:** `false` |
| 38 | + |
| 39 | +Enable or disable test coverage collection. |
| 40 | + |
| 41 | +```ts title='rstest.config.ts' |
| 42 | +import { defineConfig } from '@rstest/core'; |
| 43 | + |
| 44 | +export default defineConfig({ |
| 45 | + coverage: { |
| 46 | + enabled: true, |
| 47 | + }, |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +### provider |
| 52 | + |
| 53 | +- **Type:** `'istanbul'` |
| 54 | +- **Default:** `'istanbul'` |
| 55 | + |
| 56 | +The coverage provider to use. Currently, only [istanbul](https://istanbul.js.org/) is supported. |
| 57 | + |
| 58 | +```ts title='rstest.config.ts' |
| 59 | +import { defineConfig } from '@rstest/core'; |
| 60 | + |
| 61 | +export default defineConfig({ |
| 62 | + coverage: { |
| 63 | + enabled: true, |
| 64 | + provider: 'istanbul', |
| 65 | + }, |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +#### Istanbul provider |
| 70 | + |
| 71 | +To enable istanbul coverage, you need to install the `@rstest/coverage-istanbul` package first. |
| 72 | + |
| 73 | +import { PackageManagerTabs } from '@theme'; |
| 74 | + |
| 75 | +<PackageManagerTabs command="add @rstest/coverage-istanbul -D" /> |
| 76 | + |
| 77 | +`@rstest/coverage-istanbul` is powered by [swc-plugin-coverage-instrument](https://github.com/kwonoj/swc-plugin-coverage-instrument). |
| 78 | + |
| 79 | +### exclude |
| 80 | + |
| 81 | +- **Type:** `string[]` |
| 82 | +- **Default:** |
| 83 | + |
| 84 | +```ts |
| 85 | +[ |
| 86 | + '**/node_modules/**', |
| 87 | + '**/dist/**', |
| 88 | + '**/test/**', |
| 89 | + '**/__tests__/**', |
| 90 | + '**/__mocks__/**', |
| 91 | + '**/*.{test,spec}.[jt]s', |
| 92 | + '**/*.{test,spec}.[c|m][jt]s', |
| 93 | + '**/*.{test,spec}.[jt]sx', |
| 94 | + '**/*.{test,spec}.[c|m][jt]sx', |
| 95 | +]; |
| 96 | +``` |
| 97 | + |
| 98 | +A glob pattern array to exclude files from test coverage collection. |
| 99 | + |
| 100 | +```ts title='rstest.config.ts' |
| 101 | +import { defineConfig } from '@rstest/core'; |
| 102 | + |
| 103 | +export default defineConfig({ |
| 104 | + coverage: { |
| 105 | + enabled: true, |
| 106 | + exclude: ['**/node_modules/**', '**/dist/**'], |
| 107 | + }, |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +### reporters |
| 112 | + |
| 113 | +- **Type:** `(ReporterName | [ReporterName, ReporterOptions>])[]` |
| 114 | +- **Default:** `['text', 'html', 'clover', 'json']` |
| 115 | + |
| 116 | +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. |
| 117 | + |
| 118 | +- See [Istanbul Reporters](https://istanbul.js.org/docs/advanced/alternative-reporters/) for available reporters. |
| 119 | +- See [@types/istanbul-reporter](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/istanbul-reports/index.d.ts) for details about reporter specific options. |
| 120 | + |
| 121 | +```ts title='rstest.config.ts' |
| 122 | +import { defineConfig } from '@rstest/core'; |
| 123 | + |
| 124 | +export default defineConfig({ |
| 125 | + coverage: { |
| 126 | + enabled: true, |
| 127 | + reporters: [ |
| 128 | + 'html', |
| 129 | + ['text', { skipFull: true }], |
| 130 | + ['json', { file: 'coverage-final.json' }], |
| 131 | + ], |
| 132 | + }, |
| 133 | +}); |
| 134 | +``` |
| 135 | + |
| 136 | +### reportsDirectory |
| 137 | + |
| 138 | +- **Type:** `string` |
| 139 | +- **Default:** `'./coverage'` |
| 140 | + |
| 141 | +The directory to store coverage reports. |
| 142 | + |
| 143 | +```ts title='rstest.config.ts' |
| 144 | +import { defineConfig } from '@rstest/core'; |
| 145 | + |
| 146 | +export default defineConfig({ |
| 147 | + coverage: { |
| 148 | + enabled: true, |
| 149 | + reportsDirectory: './coverage-reports', |
| 150 | + }, |
| 151 | +}); |
| 152 | +``` |
| 153 | + |
| 154 | +### clean |
| 155 | + |
| 156 | +- **Type:** `boolean` |
| 157 | +- **Default:** `true` |
| 158 | + |
| 159 | +Whether to clean the coverage directory before running tests. |
| 160 | + |
| 161 | +```ts title='rstest.config.ts' |
| 162 | +import { defineConfig } from '@rstest/core'; |
| 163 | + |
| 164 | +export default defineConfig({ |
| 165 | + coverage: { |
| 166 | + enabled: true, |
| 167 | + clean: true, |
| 168 | + }, |
| 169 | +}); |
| 170 | +``` |
| 171 | + |
| 172 | +### thresholds |
| 173 | + |
| 174 | +- **Type:** |
| 175 | + |
| 176 | +```ts |
| 177 | +type CoverageThresholds = { |
| 178 | + /** Thresholds for statements */ |
| 179 | + statements?: number; |
| 180 | + /** Thresholds for functions */ |
| 181 | + functions?: number; |
| 182 | + /** Thresholds for branches */ |
| 183 | + branches?: number; |
| 184 | + /** Thresholds for lines */ |
| 185 | + lines?: number; |
| 186 | +}; |
| 187 | +``` |
| 188 | + |
| 189 | +- **Default:** `undefined` |
| 190 | + |
| 191 | +Coverage thresholds for enforcing minimum coverage requirements. You can set thresholds for statements, functions, branches, and lines. |
| 192 | + |
| 193 | +```ts title='rstest.config.ts' |
| 194 | +import { defineConfig } from '@rstest/core'; |
| 195 | + |
| 196 | +export default defineConfig({ |
| 197 | + coverage: { |
| 198 | + enabled: true, |
| 199 | + thresholds: { |
| 200 | + statements: 80, |
| 201 | + functions: 80, |
| 202 | + branches: 80, |
| 203 | + lines: 80, |
| 204 | + }, |
| 205 | + }, |
| 206 | +}); |
| 207 | +``` |
0 commit comments