From 6e75a5efdb6e6a9299f9a97b16fdb110f65339fa Mon Sep 17 00:00:00 2001 From: Thor Juhasz Date: Fri, 19 Jul 2024 17:38:31 +0200 Subject: [PATCH] feat(coverage): allow '100: true' for glob pattern coverage thresholds (fix #6166) --- docs/config/index.md | 25 +++++++++++ packages/vitest/src/types/coverage.ts | 2 +- packages/vitest/src/utils/coverage.ts | 9 ++++ .../test/threshold-glob-100.test.ts | 42 +++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 test/coverage-test/test/threshold-glob-100.test.ts diff --git a/docs/config/index.md b/docs/config/index.md index 0417edf7b5fb..ccb1f9009370 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -1372,6 +1372,31 @@ Sets thresholds for files matching the glob pattern. } ``` +##### coverage.thresholds[glob-pattern].100 + +- **Type:** `boolean` +- **Default:** `false` +- **Available for providers:** `'v8' | 'istanbul'` + +Sets thresholds to 100 for files matching the glob pattern. + + +```ts +{ + coverage: { + thresholds: { + // Thresholds for all files + functions: 95, + branches: 70, + + // Thresholds for matching glob pattern + 'src/utils/**.ts': { 100: true }, + '**/math.ts': { 100: true } + } + } +} +``` + #### coverage.ignoreEmptyLines - **Type:** `boolean` diff --git a/packages/vitest/src/types/coverage.ts b/packages/vitest/src/types/coverage.ts index 6062280cdea5..9bc381515b36 100644 --- a/packages/vitest/src/types/coverage.ts +++ b/packages/vitest/src/types/coverage.ts @@ -222,7 +222,7 @@ export interface BaseCoverageOptions { | ({ [glob: string]: Pick< Thresholds, - 'statements' | 'functions' | 'branches' | 'lines' + 100 | 'statements' | 'functions' | 'branches' | 'lines' > } & Thresholds) diff --git a/packages/vitest/src/utils/coverage.ts b/packages/vitest/src/utils/coverage.ts index 7b6784405ff6..6fa67711357b 100644 --- a/packages/vitest/src/utils/coverage.ts +++ b/packages/vitest/src/utils/coverage.ts @@ -305,6 +305,15 @@ function resolveGlobThresholds( return {} } + if (100 in thresholds && typeof thresholds['100'] === 'boolean' && thresholds[100] === true) { + return { + lines: 100, + branches: 100, + functions: 100, + statements: 100, + } + } + return { lines: 'lines' in thresholds && typeof thresholds.lines === 'number' diff --git a/test/coverage-test/test/threshold-glob-100.test.ts b/test/coverage-test/test/threshold-glob-100.test.ts new file mode 100644 index 000000000000..67c2190f8a5d --- /dev/null +++ b/test/coverage-test/test/threshold-glob-100.test.ts @@ -0,0 +1,42 @@ +import { expect } from 'vitest' +import { coverageTest, isV8Provider, normalizeURL, runVitest, test } from '../utils' +import { sum } from '../fixtures/src/math' +import { isEven, isOdd } from '../fixtures/src/even' + +test('{thresholds: { 100: true }} on glob pattern', async () => { + const result = await runVitest({ + include: [normalizeURL(import.meta.url)], + coverage: { + include: [ + '**/fixtures/src/even.ts', + '**/fixtures/src/math.ts', + ], + reporter: ['text'], + thresholds: { + 'branches': 100, + + // Replace these after #6170 is merged ─ Makes glob pattern files be counted toward global thresholds + // 'functions': 50, + // 'lines': isV8Provider() ? 66.66 : 50, + // 'statements': isV8Provider() ? 66.66 : 50, + 'functions': 25, + 'lines': isV8Provider() ? 50 : 25, + 'statements': isV8Provider() ? 50 : 25, + + '**/fixtures/src/even.ts': { + 100: true, + }, + }, + }, + }, { throwOnError: false }) + + expect(result.exitCode).toBe(0) +}) + +coverageTest('thresholds[\'glob-pattern\'].100 sets thresholds to 100 for said glob pattern', () => { + expect(sum(1, 2)).toBe(3) + expect(isEven(2)).toBe(true) + expect(isEven(1)).toBe(false) + expect(isOdd(3)).toBe(true) + expect(isOdd(4)).toBe(false) +})