Skip to content

Commit 02e3f00

Browse files
thor-juhaszAriPerkkio
andauthoredJul 21, 2024··
fix(coverage): global thresholds to include files from glob thresholds (#6172)
Co-authored-by: Ari Perkkiö <ari.perkkio@gmail.com>
1 parent 807a2cb commit 02e3f00

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed
 

‎docs/config/index.md

+5
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,11 @@ Shortcut for `--coverage.thresholds.lines 100 --coverage.thresholds.functions 10
13451345

13461346
Sets thresholds for files matching the glob pattern.
13471347

1348+
::: tip NOTE
1349+
Vitest counts all files, including those covered by glob-patterns, into the global coverage thresholds.
1350+
This is different from Jest behavior.
1351+
:::
1352+
13481353
<!-- eslint-skip -->
13491354
```ts
13501355
{

‎packages/vitest/src/utils/coverage.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ export class BaseCoverageProvider {
182182
}): ResolvedThreshold[] {
183183
const resolvedThresholds: ResolvedThreshold[] = []
184184
const files = coverageMap.files()
185-
const filesMatchedByGlobs: string[] = []
186185
const globalCoverageMap = createCoverageMap()
187186

188187
for (const key of Object.keys(
@@ -204,7 +203,6 @@ export class BaseCoverageProvider {
204203
const matchingFiles = files.filter(file =>
205204
mm.isMatch(relative(root, file), glob),
206205
)
207-
filesMatchedByGlobs.push(...matchingFiles)
208206

209207
for (const file of matchingFiles) {
210208
const fileCoverage = coverageMap.fileCoverageFor(file)
@@ -218,10 +216,8 @@ export class BaseCoverageProvider {
218216
})
219217
}
220218

221-
// Global threshold is for all files that were not included by glob patterns
222-
for (const file of files.filter(
223-
file => !filesMatchedByGlobs.includes(file),
224-
)) {
219+
// Global threshold is for all files, even if they are included by glob patterns
220+
for (const file of files) {
225221
const fileCoverage = coverageMap.fileCoverageFor(file)
226222
globalCoverageMap.addFileCoverage(fileCoverage)
227223
}

‎test/coverage-test/test/threshold-auto-update.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ test('thresholds.autoUpdate updates thresholds', async () => {
5353
autoUpdate: true,
5454
5555
// Global ones
56-
lines: 66.66,
57-
functions: 50,
56+
lines: 55.55,
57+
functions: 33.33,
5858
branches: 100,
59-
statements: 66.66,
59+
statements: 55.55,
6060
6161
'**/src/math.ts': {
6262
branches: 100,
@@ -81,10 +81,10 @@ test('thresholds.autoUpdate updates thresholds', async () => {
8181
autoUpdate: true,
8282
8383
// Global ones
84-
lines: 50,
85-
functions: 50,
84+
lines: 33.33,
85+
functions: 33.33,
8686
branches: 100,
87-
statements: 50,
87+
statements: 33.33,
8888
8989
'**/src/math.ts': {
9090
branches: 100,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect } from 'vitest'
2+
import { coverageTest, isV8Provider, normalizeURL, runVitest, test } from '../utils'
3+
import { sum } from '../fixtures/src/math'
4+
import { isEven, isOdd } from '../fixtures/src/even'
5+
6+
test('threshold glob patterns count in global coverage', async () => {
7+
await runVitest({
8+
include: [normalizeURL(import.meta.url)],
9+
coverage: {
10+
all: false,
11+
include: ['**/fixtures/src/**'],
12+
thresholds: {
13+
'branches': 100,
14+
'functions': 50,
15+
'lines': isV8Provider() ? 66 : 50,
16+
'statements': isV8Provider() ? 66 : 50,
17+
18+
'**/fixtures/src/even.ts': {
19+
branches: 100,
20+
functions: 100,
21+
lines: 100,
22+
statements: 100,
23+
},
24+
},
25+
},
26+
})
27+
})
28+
29+
coverageTest('cover some lines, but not too much', () => {
30+
expect(sum(1, 2)).toBe(3)
31+
expect(isEven(4)).toBe(true)
32+
expect(isOdd(4)).toBe(false)
33+
})

0 commit comments

Comments
 (0)
Please sign in to comment.