Skip to content

Commit f68453f

Browse files
feat(coverage): add thresholds.<glob>.100 option (#6174)
Co-authored-by: Ari Perkkiö <[email protected]>
1 parent 02e3f00 commit f68453f

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

Diff for: docs/config/index.md

+25
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,31 @@ This is different from Jest behavior.
13771377
}
13781378
```
13791379

1380+
##### coverage.thresholds[glob-pattern].100
1381+
1382+
- **Type:** `boolean`
1383+
- **Default:** `false`
1384+
- **Available for providers:** `'v8' | 'istanbul'`
1385+
1386+
Sets thresholds to 100 for files matching the glob pattern.
1387+
1388+
<!-- eslint-skip -->
1389+
```ts
1390+
{
1391+
coverage: {
1392+
thresholds: {
1393+
// Thresholds for all files
1394+
functions: 95,
1395+
branches: 70,
1396+
1397+
// Thresholds for matching glob pattern
1398+
'src/utils/**.ts': { 100: true },
1399+
'**/math.ts': { 100: true }
1400+
}
1401+
}
1402+
}
1403+
```
1404+
13801405
#### coverage.ignoreEmptyLines
13811406

13821407
- **Type:** `boolean`

Diff for: packages/vitest/src/types/coverage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export interface BaseCoverageOptions {
222222
| ({
223223
[glob: string]: Pick<
224224
Thresholds,
225-
'statements' | 'functions' | 'branches' | 'lines'
225+
100 | 'statements' | 'functions' | 'branches' | 'lines'
226226
>
227227
} & Thresholds)
228228

Diff for: packages/vitest/src/utils/coverage.ts

+9
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,15 @@ function resolveGlobThresholds(
301301
return {}
302302
}
303303

304+
if (100 in thresholds && thresholds[100] === true) {
305+
return {
306+
lines: 100,
307+
branches: 100,
308+
functions: 100,
309+
statements: 100,
310+
}
311+
}
312+
304313
return {
305314
lines:
306315
'lines' in thresholds && typeof thresholds.lines === 'number'

Diff for: test/coverage-test/test/configuration-options.test-d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ test('provider options, generic', () => {
3838
'statements': 100,
3939

4040
'**/some-file.ts': {
41+
100: true,
4142
lines: 12,
4243
branches: 12,
4344
functions: 12,
@@ -54,9 +55,14 @@ test('provider options, generic', () => {
5455
statements: [80, 95],
5556
},
5657
thresholds: {
57-
'100': true,
58+
'100': false,
59+
'lines': 1,
60+
'autoUpdate': true,
61+
'perFile': true,
62+
'statements': 100,
5863

5964
'**/some-file.ts': {
65+
100: false,
6066
lines: 12,
6167
branches: 12,
6268
functions: 12,

Diff for: test/coverage-test/test/threshold-glob.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,45 @@ test('threshold glob patterns count in global coverage', async () => {
2626
})
2727
})
2828

29+
test('{ thresholds: { 100: true } } on glob pattern', async () => {
30+
const { stderr, exitCode } = await runVitest({
31+
include: [normalizeURL(import.meta.url)],
32+
coverage: {
33+
include: [
34+
'**/fixtures/src/even.ts',
35+
'**/fixtures/src/math.ts',
36+
],
37+
thresholds: {
38+
'**/fixtures/src/even.ts': {
39+
100: true,
40+
},
41+
'**/fixtures/src/math.ts': {
42+
100: true,
43+
},
44+
},
45+
},
46+
}, { throwOnError: false })
47+
48+
expect(exitCode).toBe(1)
49+
50+
if (isV8Provider()) {
51+
expect(stderr).toMatchInlineSnapshot(`
52+
"ERROR: Coverage for lines (50%) does not meet "**/fixtures/src/math.ts" threshold (100%)
53+
ERROR: Coverage for functions (25%) does not meet "**/fixtures/src/math.ts" threshold (100%)
54+
ERROR: Coverage for statements (50%) does not meet "**/fixtures/src/math.ts" threshold (100%)
55+
"
56+
`)
57+
}
58+
else {
59+
expect(stderr).toMatchInlineSnapshot(`
60+
"ERROR: Coverage for lines (25%) does not meet "**/fixtures/src/math.ts" threshold (100%)
61+
ERROR: Coverage for functions (25%) does not meet "**/fixtures/src/math.ts" threshold (100%)
62+
ERROR: Coverage for statements (25%) does not meet "**/fixtures/src/math.ts" threshold (100%)
63+
"
64+
`)
65+
}
66+
})
67+
2968
coverageTest('cover some lines, but not too much', () => {
3069
expect(sum(1, 2)).toBe(3)
3170
expect(isEven(4)).toBe(true)

0 commit comments

Comments
 (0)