Skip to content

Commit

Permalink
feat(coverage): add thresholds.<glob>.100 option (#6174)
Browse files Browse the repository at this point in the history
Co-authored-by: Ari Perkkiö <[email protected]>
  • Loading branch information
thor-juhasz and AriPerkkio authored Jul 21, 2024
1 parent 02e3f00 commit f68453f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
25 changes: 25 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- eslint-skip -->
```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`
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/types/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export interface BaseCoverageOptions {
| ({
[glob: string]: Pick<
Thresholds,
'statements' | 'functions' | 'branches' | 'lines'
100 | 'statements' | 'functions' | 'branches' | 'lines'
>
} & Thresholds)

Expand Down
9 changes: 9 additions & 0 deletions packages/vitest/src/utils/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 7 additions & 1 deletion test/coverage-test/test/configuration-options.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ test('provider options, generic', () => {
'statements': 100,

'**/some-file.ts': {
100: true,
lines: 12,
branches: 12,
functions: 12,
Expand All @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions test/coverage-test/test/threshold-glob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f68453f

Please sign in to comment.