From f68453f8918580e408a7d7ab7a4770e95982f5a6 Mon Sep 17 00:00:00 2001 From: Thor Juhasz Date: Sun, 21 Jul 2024 16:20:00 +0200 Subject: [PATCH] feat(coverage): add `thresholds..100` option (#6174) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ari Perkkiƶ --- docs/config/index.md | 25 ++++++++++++ packages/vitest/src/types/coverage.ts | 2 +- packages/vitest/src/utils/coverage.ts | 9 +++++ .../test/configuration-options.test-d.ts | 8 +++- .../coverage-test/test/threshold-glob.test.ts | 39 +++++++++++++++++++ 5 files changed, 81 insertions(+), 2 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index a03d43b8b991..a8c596c2f135 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -1377,6 +1377,31 @@ This is different from Jest behavior. } ``` +##### 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 bdc729bf5985..b8ac873f2d77 100644 --- a/packages/vitest/src/utils/coverage.ts +++ b/packages/vitest/src/utils/coverage.ts @@ -301,6 +301,15 @@ function resolveGlobThresholds( return {} } + if (100 in thresholds && 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/configuration-options.test-d.ts b/test/coverage-test/test/configuration-options.test-d.ts index 5b3b8c1f7db3..67d33be7cdfa 100644 --- a/test/coverage-test/test/configuration-options.test-d.ts +++ b/test/coverage-test/test/configuration-options.test-d.ts @@ -38,6 +38,7 @@ test('provider options, generic', () => { 'statements': 100, '**/some-file.ts': { + 100: true, lines: 12, branches: 12, functions: 12, @@ -54,9 +55,14 @@ test('provider options, generic', () => { statements: [80, 95], }, thresholds: { - '100': true, + '100': false, + 'lines': 1, + 'autoUpdate': true, + 'perFile': true, + 'statements': 100, '**/some-file.ts': { + 100: false, lines: 12, branches: 12, functions: 12, diff --git a/test/coverage-test/test/threshold-glob.test.ts b/test/coverage-test/test/threshold-glob.test.ts index 2f7625a3a080..d1cba59ae38d 100644 --- a/test/coverage-test/test/threshold-glob.test.ts +++ b/test/coverage-test/test/threshold-glob.test.ts @@ -26,6 +26,45 @@ test('threshold glob patterns count in global coverage', async () => { }) }) +test('{ thresholds: { 100: true } } on glob pattern', async () => { + const { stderr, exitCode } = await runVitest({ + include: [normalizeURL(import.meta.url)], + coverage: { + include: [ + '**/fixtures/src/even.ts', + '**/fixtures/src/math.ts', + ], + thresholds: { + '**/fixtures/src/even.ts': { + 100: true, + }, + '**/fixtures/src/math.ts': { + 100: true, + }, + }, + }, + }, { throwOnError: false }) + + expect(exitCode).toBe(1) + + if (isV8Provider()) { + expect(stderr).toMatchInlineSnapshot(` + "ERROR: Coverage for lines (50%) does not meet "**/fixtures/src/math.ts" threshold (100%) + ERROR: Coverage for functions (25%) does not meet "**/fixtures/src/math.ts" threshold (100%) + ERROR: Coverage for statements (50%) does not meet "**/fixtures/src/math.ts" threshold (100%) + " + `) + } + else { + expect(stderr).toMatchInlineSnapshot(` + "ERROR: Coverage for lines (25%) does not meet "**/fixtures/src/math.ts" threshold (100%) + ERROR: Coverage for functions (25%) does not meet "**/fixtures/src/math.ts" threshold (100%) + ERROR: Coverage for statements (25%) does not meet "**/fixtures/src/math.ts" threshold (100%) + " + `) + } +}) + coverageTest('cover some lines, but not too much', () => { expect(sum(1, 2)).toBe(3) expect(isEven(4)).toBe(true)