From 02e3f00370557dda38a94c15ca542e38e5ce1f7a Mon Sep 17 00:00:00 2001 From: Thor Juhasz Date: Sun, 21 Jul 2024 15:51:19 +0200 Subject: [PATCH] fix(coverage): global thresholds to include files from glob thresholds (#6172) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ari Perkkiƶ --- docs/config/index.md | 5 +++ packages/vitest/src/utils/coverage.ts | 8 ++--- .../test/threshold-auto-update.test.ts | 12 +++---- .../coverage-test/test/threshold-glob.test.ts | 33 +++++++++++++++++++ 4 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 test/coverage-test/test/threshold-glob.test.ts diff --git a/docs/config/index.md b/docs/config/index.md index 0417edf7b5fb..a03d43b8b991 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -1345,6 +1345,11 @@ Shortcut for `--coverage.thresholds.lines 100 --coverage.thresholds.functions 10 Sets thresholds for files matching the glob pattern. +::: tip NOTE +Vitest counts all files, including those covered by glob-patterns, into the global coverage thresholds. +This is different from Jest behavior. +::: + ```ts { diff --git a/packages/vitest/src/utils/coverage.ts b/packages/vitest/src/utils/coverage.ts index 7b6784405ff6..bdc729bf5985 100644 --- a/packages/vitest/src/utils/coverage.ts +++ b/packages/vitest/src/utils/coverage.ts @@ -182,7 +182,6 @@ export class BaseCoverageProvider { }): ResolvedThreshold[] { const resolvedThresholds: ResolvedThreshold[] = [] const files = coverageMap.files() - const filesMatchedByGlobs: string[] = [] const globalCoverageMap = createCoverageMap() for (const key of Object.keys( @@ -204,7 +203,6 @@ export class BaseCoverageProvider { const matchingFiles = files.filter(file => mm.isMatch(relative(root, file), glob), ) - filesMatchedByGlobs.push(...matchingFiles) for (const file of matchingFiles) { const fileCoverage = coverageMap.fileCoverageFor(file) @@ -218,10 +216,8 @@ export class BaseCoverageProvider { }) } - // Global threshold is for all files that were not included by glob patterns - for (const file of files.filter( - file => !filesMatchedByGlobs.includes(file), - )) { + // Global threshold is for all files, even if they are included by glob patterns + for (const file of files) { const fileCoverage = coverageMap.fileCoverageFor(file) globalCoverageMap.addFileCoverage(fileCoverage) } diff --git a/test/coverage-test/test/threshold-auto-update.test.ts b/test/coverage-test/test/threshold-auto-update.test.ts index cbce27e170cb..f39ea10158b9 100644 --- a/test/coverage-test/test/threshold-auto-update.test.ts +++ b/test/coverage-test/test/threshold-auto-update.test.ts @@ -53,10 +53,10 @@ test('thresholds.autoUpdate updates thresholds', async () => { autoUpdate: true, // Global ones - lines: 66.66, - functions: 50, + lines: 55.55, + functions: 33.33, branches: 100, - statements: 66.66, + statements: 55.55, '**/src/math.ts': { branches: 100, @@ -81,10 +81,10 @@ test('thresholds.autoUpdate updates thresholds', async () => { autoUpdate: true, // Global ones - lines: 50, - functions: 50, + lines: 33.33, + functions: 33.33, branches: 100, - statements: 50, + statements: 33.33, '**/src/math.ts': { branches: 100, diff --git a/test/coverage-test/test/threshold-glob.test.ts b/test/coverage-test/test/threshold-glob.test.ts new file mode 100644 index 000000000000..2f7625a3a080 --- /dev/null +++ b/test/coverage-test/test/threshold-glob.test.ts @@ -0,0 +1,33 @@ +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('threshold glob patterns count in global coverage', async () => { + await runVitest({ + include: [normalizeURL(import.meta.url)], + coverage: { + all: false, + include: ['**/fixtures/src/**'], + thresholds: { + 'branches': 100, + 'functions': 50, + 'lines': isV8Provider() ? 66 : 50, + 'statements': isV8Provider() ? 66 : 50, + + '**/fixtures/src/even.ts': { + branches: 100, + functions: 100, + lines: 100, + statements: 100, + }, + }, + }, + }) +}) + +coverageTest('cover some lines, but not too much', () => { + expect(sum(1, 2)).toBe(3) + expect(isEven(4)).toBe(true) + expect(isOdd(4)).toBe(false) +})