Skip to content

Commit a29fece

Browse files
authored
fix(coverage): thresholdAutoUpdate to detect zero limits (#4287)
1 parent 6b73473 commit a29fece

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

packages/vitest/src/utils/coverage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class BaseCoverageProvider {
3030
const thresholdsToUpdate: [Threshold, number][] = []
3131

3232
for (const key of THRESHOLD_KEYS) {
33-
const threshold = thresholds[key] || 100
33+
const threshold = thresholds[key] ?? 100
3434
const actual = Math.min(...summaries.map(summary => summary[key].pct))
3535

3636
if (actual > threshold)
@@ -45,7 +45,7 @@ export class BaseCoverageProvider {
4545

4646
for (const [threshold, newValue] of thresholdsToUpdate) {
4747
// Find the exact match from the configuration file and replace the value
48-
const previousThreshold = (thresholds[threshold] || 100).toString()
48+
const previousThreshold = (thresholds[threshold] ?? 100).toString()
4949
const pattern = new RegExp(`(${threshold}\\s*:\\s*)${previousThreshold.replace('.', '\\.')}`)
5050
const matches = originalConfig.match(pattern)
5151

test/coverage-test/coverage-report-tests/generic.report.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ test('thresholdAutoUpdate updates thresholds', async () => {
7575
const match = configContents.match(new RegExp(`${threshold}: (?<coverage>[\\d|\\.]+)`))
7676
const coverage = match?.groups?.coverage || '0'
7777

78-
// Configuration has fixed value of 1.01 set for each threshold
78+
// Configuration has fixed value of 1.01 and 0 set for each threshold
7979
expect(Number.parseInt(coverage)).toBeGreaterThan(1.01)
8080
}
8181

8282
// Update thresholds back to fixed values
83-
const updatedConfig = configContents.replace(/(branches|functions|lines|statements): ([\d|\.])+/g, '$1: 1.01')
83+
const updatedConfig = configContents
84+
.replace(/(branches|statements): ([\d|\.])+/g, '$1: 1.01')
85+
.replace(/(functions|lines): ([\d|\.])+/g, '$1: 0')
8486
fs.writeFileSync(configFilename, updatedConfig)
8587
})
8688

test/coverage-test/vitest.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ export default defineConfig({
8383

8484
// These will be updated by tests and reseted back by generic.report.test.ts
8585
thresholdAutoUpdate: true,
86-
functions: 1.01,
86+
functions: 0,
8787
branches: 1.01,
88-
lines: 1.01,
88+
lines: 0,
8989
statements: 1.01,
9090
},
9191
setupFiles: [

0 commit comments

Comments
 (0)