Skip to content

Commit

Permalink
fix(coverage): global thresholds to include files from glob thresholds (
Browse files Browse the repository at this point in the history
#6172)

Co-authored-by: Ari Perkkiö <[email protected]>
  • Loading branch information
thor-juhasz and AriPerkkio authored Jul 21, 2024
1 parent 807a2cb commit 02e3f00
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
5 changes: 5 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
:::

<!-- eslint-skip -->
```ts
{
Expand Down
8 changes: 2 additions & 6 deletions packages/vitest/src/utils/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions test/coverage-test/test/threshold-auto-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions test/coverage-test/test/threshold-glob.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit 02e3f00

Please sign in to comment.