Skip to content

Commit bcf195a

Browse files
authored
Merge pull request #996 from vitest-dev/sync-89a0dbd2-1
docs(en): merge docs-cn/sync-docs into docs-cn/dev @ 89a0dbd
2 parents 287d45f + 371e030 commit bcf195a

3 files changed

Lines changed: 87 additions & 10 deletions

File tree

config/coverage.md

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,67 @@ npx vitest --coverage.enabled --coverage.provider=istanbul
233233

234234
### coverage.thresholds.perFile
235235

236-
- **类型:** `boolean`
236+
- **类型:** `boolean | { 100?: boolean, lines?: number, functions?: number, branches?: number, statements?: number }`
237237
- **默认值:** `false`
238238
- **可用的测试提供者:** `'v8' | 'istanbul'`
239239
- **命令行终端:** `--coverage.thresholds.perFile`, `--coverage.thresholds.perFile=false`
240+
<!-- TODO: translation -->
241+
When `true`, each file is checked against the top-level thresholds instead of the project-wide aggregate. When set to an object, both are checked: the aggregate against the top-level thresholds, and every file against these per-file minimums.
240242

241-
按文件检查覆盖率阈值。
243+
<!-- eslint-skip -->
244+
```ts
245+
{
246+
coverage: {
247+
thresholds: {
248+
lines: 80,
249+
functions: 80,
250+
branches: 80,
251+
statements: 80,
252+
perFile: {
253+
lines: 50,
254+
functions: 50,
255+
branches: 50,
256+
statements: 50,
257+
},
258+
}
259+
}
260+
}
261+
```
262+
263+
`{ 100: true }` is also accepted inside the object as a shortcut for setting all four metrics to `100`:
264+
265+
<!-- eslint-skip -->
266+
```ts
267+
{
268+
coverage: {
269+
thresholds: {
270+
lines: 80,
271+
perFile: {
272+
100: true,
273+
},
274+
}
275+
}
276+
}
277+
```
278+
279+
`perFile` can also be set on an individual [glob-pattern threshold](/config/coverage#coverage-thresholds-glob-pattern). Glob patterns do **not** inherit the top-level `perFile`; set it on each glob explicitly.
280+
281+
<!-- eslint-skip -->
282+
```ts
283+
{
284+
coverage: {
285+
thresholds: {
286+
perFile: true,
287+
lines: 80,
288+
289+
'src/utils/**': {
290+
lines: 90,
291+
perFile: true,
292+
},
293+
}
294+
}
295+
}
296+
```
242297

243298
### coverage.thresholds.autoUpdate
244299

@@ -249,17 +304,14 @@ npx vitest --coverage.enabled --coverage.provider=istanbul
249304

250305
当实际覆盖率超过配置阈值时,自动将 `lines``functions``branches``statements` 的阈值更新到配置文件中。
251306
此选项适用于覆盖率提高时保持阈值不变。
252-
<!-- TODO: translation -->
307+
253308
你也可以通过传入函数自定义阈值更新值的格式,The function receives the new threshold as the first argument and the previous threshold as the second:
254309

255310
<!-- eslint-skip -->
256311
```ts
257312
{
258313
coverage: {
259314
thresholds: {
260-
// 更新阈值为整数
261-
autoUpdate: (newThreshold) => Math.floor(newThreshold),
262-
263315
// Log the change and update without decimals
264316
autoUpdate: (newThreshold, previousThreshold) => {
265317
console.log(`Updated threshold from ${previousThreshold} to ${newThreshold}`)
@@ -285,11 +337,13 @@ npx vitest --coverage.enabled --coverage.provider=istanbul
285337

286338
### coverage.thresholds[glob-pattern]
287339

288-
- **类型:** `{ statements?: number functions?: number branches?: number lines?: number }`
340+
- **类型:** `{ statements?: number, functions?: number, branches?: number, lines?: number, perFile?: boolean | object }`
289341
- **默认值:** `undefined`
290342
- **可用的测试提供者:** `'v8' | 'istanbul'`
291343

292344
设置与 glob 模式匹配的文件的阈值。
345+
<!-- TODO: translation -->
346+
Each glob pattern can set its own `perFile` (`boolean | object`), checked exactly like the top-level `perFile` but scoped to the matched files. Glob patterns do not inherit the top-level `perFile` — set it per glob.
293347

294348
::: tip 注意
295349
Vitest 会将所有文件(包括匹配 glob 模式的文件)计入全局覆盖率阈值计算。
@@ -311,6 +365,8 @@ Vitest 会将所有文件(包括匹配 glob 模式的文件)计入全局覆
311365
functions: 90,
312366
branches: 85,
313367
lines: 80,
368+
// each matching file must individually hit the thresholds above
369+
perFile: true,
314370
},
315371

316372
// 匹配此模式的文件仅设置行覆盖率阈值

guide/cli-generated.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ Coverage reporters to use. Visit [`coverage.reporter`](/config/coverage#coverage
197197

198198
### coverage.thresholds.perFile
199199

200-
- **命令行终端:** `--coverage.thresholds.perFile`
200+
- **命令行终端:** `--coverage.thresholds.perFile <boolean>`
201201
- **配置:** [coverage.thresholds.perFile](/config/coverage#coverage-thresholds-perfile)
202-
203-
检查每个文件的阈值。 `--coverage.thresholds.lines`, `--coverage.thresholds.functions`, `--coverage.thresholds.branches`, `--coverage.thresholds.statements` 为实际阈值(默认值:`false`
202+
<!-- TODO: translation -->
203+
检查每个文件的阈值。 `--coverage.thresholds.lines`, `--coverage.thresholds.functions`, `--coverage.thresholds.branches`, `--coverage.thresholds.statements` 为实际阈值(默认值:`false`。Object form is available in config files only.
204204

205205
### coverage.thresholds.autoUpdate
206206

guide/migration.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ await expect.element(banner).toMatchTextContent(/error/i) // [!code ++]
125125
await expect.element(banner).toHaveTextContent('Error!')
126126
```
127127

128+
### Glob Coverage Thresholds No Longer Inherit `perFile`
129+
130+
`coverage.thresholds.perFile` previously applied to every threshold set, including files matched by glob-pattern thresholds. Glob patterns now control their own per-file checking and no longer inherit the top-level `perFile` — set `perFile` on each glob that needs it.
131+
132+
```ts [vitest.config.ts]
133+
export default defineConfig({
134+
test: {
135+
coverage: {
136+
thresholds: {
137+
'perFile': true,
138+
139+
'src/utils/**': {
140+
lines: 80,
141+
perFile: true, // [!code ++]
142+
},
143+
},
144+
},
145+
},
146+
})
147+
```
148+
128149
### Config Files Are Not Looked Up From Parent Directories
129150

130151
Vitest no longer searches parent directories for config files. If you previously relied on running `vitest` from a subdirectory while using a config file from a parent directory, pass the config explicitly and scope test discovery with `--dir`. For example,

0 commit comments

Comments
 (0)