diff --git a/packages/vitest/src/utils/coverage.ts b/packages/vitest/src/utils/coverage.ts index 7379243fd584..28d1f601de5d 100644 --- a/packages/vitest/src/utils/coverage.ts +++ b/packages/vitest/src/utils/coverage.ts @@ -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) @@ -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) diff --git a/test/coverage-test/coverage-report-tests/generic.report.test.ts b/test/coverage-test/coverage-report-tests/generic.report.test.ts index fd2102756671..ecec122ed0ca 100644 --- a/test/coverage-test/coverage-report-tests/generic.report.test.ts +++ b/test/coverage-test/coverage-report-tests/generic.report.test.ts @@ -75,12 +75,14 @@ test('thresholdAutoUpdate updates thresholds', async () => { const match = configContents.match(new RegExp(`${threshold}: (?[\\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) }) diff --git a/test/coverage-test/vitest.config.ts b/test/coverage-test/vitest.config.ts index abf3c1e21afd..7f5fc5c7a65c 100644 --- a/test/coverage-test/vitest.config.ts +++ b/test/coverage-test/vitest.config.ts @@ -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: [