diff --git a/config/coverage.md b/config/coverage.md index 68ac8fea..f91ac274 100644 --- a/config/coverage.md +++ b/config/coverage.md @@ -233,12 +233,67 @@ npx vitest --coverage.enabled --coverage.provider=istanbul ### coverage.thresholds.perFile -- **类型:** `boolean` +- **类型:** `boolean | { 100?: boolean, lines?: number, functions?: number, branches?: number, statements?: number }` - **默认值:** `false` - **可用的测试提供者:** `'v8' | 'istanbul'` - **命令行终端:** `--coverage.thresholds.perFile`, `--coverage.thresholds.perFile=false` + +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. -按文件检查覆盖率阈值。 + +```ts +{ + coverage: { + thresholds: { + lines: 80, + functions: 80, + branches: 80, + statements: 80, + perFile: { + lines: 50, + functions: 50, + branches: 50, + statements: 50, + }, + } + } +} +``` + +`{ 100: true }` is also accepted inside the object as a shortcut for setting all four metrics to `100`: + + +```ts +{ + coverage: { + thresholds: { + lines: 80, + perFile: { + 100: true, + }, + } + } +} +``` + +`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. + + +```ts +{ + coverage: { + thresholds: { + perFile: true, + lines: 80, + + 'src/utils/**': { + lines: 90, + perFile: true, + }, + } + } +} +``` ### coverage.thresholds.autoUpdate @@ -249,7 +304,7 @@ npx vitest --coverage.enabled --coverage.provider=istanbul 当实际覆盖率超过配置阈值时,自动将 `lines`、`functions`、`branches` 和 `statements` 的阈值更新到配置文件中。 此选项适用于覆盖率提高时保持阈值不变。 - + 你也可以通过传入函数自定义阈值更新值的格式,The function receives the new threshold as the first argument and the previous threshold as the second: @@ -257,9 +312,6 @@ npx vitest --coverage.enabled --coverage.provider=istanbul { coverage: { thresholds: { - // 更新阈值为整数 - autoUpdate: (newThreshold) => Math.floor(newThreshold), - // Log the change and update without decimals autoUpdate: (newThreshold, previousThreshold) => { console.log(`Updated threshold from ${previousThreshold} to ${newThreshold}`) @@ -285,11 +337,13 @@ npx vitest --coverage.enabled --coverage.provider=istanbul ### coverage.thresholds[glob-pattern] -- **类型:** `{ statements?: number functions?: number branches?: number lines?: number }` +- **类型:** `{ statements?: number, functions?: number, branches?: number, lines?: number, perFile?: boolean | object }` - **默认值:** `undefined` - **可用的测试提供者:** `'v8' | 'istanbul'` 设置与 glob 模式匹配的文件的阈值。 + +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. ::: tip 注意 Vitest 会将所有文件(包括匹配 glob 模式的文件)计入全局覆盖率阈值计算。 @@ -311,6 +365,8 @@ Vitest 会将所有文件(包括匹配 glob 模式的文件)计入全局覆 functions: 90, branches: 85, lines: 80, + // each matching file must individually hit the thresholds above + perFile: true, }, // 匹配此模式的文件仅设置行覆盖率阈值 diff --git a/guide/cli-generated.md b/guide/cli-generated.md index 9a0c9c0d..1eb8cecb 100644 --- a/guide/cli-generated.md +++ b/guide/cli-generated.md @@ -197,10 +197,10 @@ Coverage reporters to use. Visit [`coverage.reporter`](/config/coverage#coverage ### coverage.thresholds.perFile -- **命令行终端:** `--coverage.thresholds.perFile` +- **命令行终端:** `--coverage.thresholds.perFile ` - **配置:** [coverage.thresholds.perFile](/config/coverage#coverage-thresholds-perfile) - -检查每个文件的阈值。 `--coverage.thresholds.lines`, `--coverage.thresholds.functions`, `--coverage.thresholds.branches`, `--coverage.thresholds.statements` 为实际阈值(默认值:`false`) + +检查每个文件的阈值。 `--coverage.thresholds.lines`, `--coverage.thresholds.functions`, `--coverage.thresholds.branches`, `--coverage.thresholds.statements` 为实际阈值(默认值:`false`)。Object form is available in config files only. ### coverage.thresholds.autoUpdate diff --git a/guide/migration.md b/guide/migration.md index 1758c866..5ce3bf4b 100644 --- a/guide/migration.md +++ b/guide/migration.md @@ -125,6 +125,27 @@ await expect.element(banner).toMatchTextContent(/error/i) // [!code ++] await expect.element(banner).toHaveTextContent('Error!') ``` +### Glob Coverage Thresholds No Longer Inherit `perFile` + +`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. + +```ts [vitest.config.ts] +export default defineConfig({ + test: { + coverage: { + thresholds: { + 'perFile': true, + + 'src/utils/**': { + lines: 80, + perFile: true, // [!code ++] + }, + }, + }, + }, +}) +``` + ### Config Files Are Not Looked Up From Parent Directories 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,