Skip to content

Commit

Permalink
fix(coverage): thresholdAutoUpdate to detect zero limits (#4287)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Oct 23, 2023
1 parent 6b73473 commit a29fece
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/utils/coverage.ts
Expand Up @@ -30,7 +30,7 @@ export class BaseCoverageProvider {
const thresholdsToUpdate: [Threshold, number][] = []

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

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

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

Expand Down
Expand Up @@ -75,12 +75,14 @@ test('thresholdAutoUpdate updates thresholds', async () => {
const match = configContents.match(new RegExp(`${threshold}: (?<coverage>[\\d|\\.]+)`))
const coverage = match?.groups?.coverage || '0'

// Configuration has fixed value of 1.01 set for each threshold
// Configuration has fixed value of 1.01 and 0 set for each threshold
expect(Number.parseInt(coverage)).toBeGreaterThan(1.01)
}

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

Expand Down
4 changes: 2 additions & 2 deletions test/coverage-test/vitest.config.ts
Expand Up @@ -83,9 +83,9 @@ export default defineConfig({

// These will be updated by tests and reseted back by generic.report.test.ts
thresholdAutoUpdate: true,
functions: 1.01,
functions: 0,
branches: 1.01,
lines: 1.01,
lines: 0,
statements: 1.01,
},
setupFiles: [
Expand Down

0 comments on commit a29fece

Please sign in to comment.