Skip to content

Commit

Permalink
feat(coverage): allow '100: true' for glob pattern coverage thresholds (
Browse files Browse the repository at this point in the history
fix #6166)
  • Loading branch information
thor-juhasz committed Jul 19, 2024
1 parent 807a2cb commit 6e75a5e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
25 changes: 25 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,31 @@ Sets thresholds for files matching the glob pattern.
}
```

##### 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 @@ -305,6 +305,15 @@ function resolveGlobThresholds(
return {}
}

if (100 in thresholds && typeof thresholds['100'] === 'boolean' && thresholds[100] === true) {
return {
lines: 100,
branches: 100,
functions: 100,
statements: 100,
}
}

return {
lines:
'lines' in thresholds && typeof thresholds.lines === 'number'
Expand Down
42 changes: 42 additions & 0 deletions test/coverage-test/test/threshold-glob-100.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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('{thresholds: { 100: true }} on glob pattern', async () => {
const result = await runVitest({
include: [normalizeURL(import.meta.url)],
coverage: {
include: [
'**/fixtures/src/even.ts',
'**/fixtures/src/math.ts',
],
reporter: ['text'],
thresholds: {
'branches': 100,

// Replace these after #6170 is merged ─ Makes glob pattern files be counted toward global thresholds
// 'functions': 50,
// 'lines': isV8Provider() ? 66.66 : 50,
// 'statements': isV8Provider() ? 66.66 : 50,
'functions': 25,
'lines': isV8Provider() ? 50 : 25,
'statements': isV8Provider() ? 50 : 25,

'**/fixtures/src/even.ts': {
100: true,
},
},
},
}, { throwOnError: false })

expect(result.exitCode).toBe(0)
})

coverageTest('thresholds[\'glob-pattern\'].100 sets thresholds to 100 for said glob pattern', () => {
expect(sum(1, 2)).toBe(3)
expect(isEven(2)).toBe(true)
expect(isEven(1)).toBe(false)
expect(isOdd(3)).toBe(true)
expect(isOdd(4)).toBe(false)
})

0 comments on commit 6e75a5e

Please sign in to comment.