Skip to content

Commit 29eebf6

Browse files
authoredApr 12, 2023
fix(coverage): thresholdAutoUpdate to work with perFile (#3182)
1 parent baf902a commit 29eebf6

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed
 

‎packages/coverage-c8/src/provider.ts

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ export class C8CoverageProvider extends BaseCoverageProvider implements Coverage
187187
lines: this.options.lines,
188188
statements: this.options.statements,
189189
},
190+
perFile: this.options.perFile,
190191
configurationFile: this.ctx.server.config.configFile,
191192
})
192193
}

‎packages/coverage-istanbul/src/provider.ts

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
164164
lines: this.options.lines,
165165
statements: this.options.statements,
166166
},
167+
perFile: this.options.perFile,
167168
configurationFile: this.ctx.server.config.configFile,
168169
})
169170
}

‎packages/vitest/src/utils/coverage.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,30 @@ export class BaseCoverageProvider {
1010
/**
1111
* Check if current coverage is above configured thresholds and bump the thresholds if needed
1212
*/
13-
updateThresholds({ configurationFile, coverageMap, thresholds }: {
13+
updateThresholds({ configurationFile, coverageMap, thresholds, perFile }: {
1414
coverageMap: CoverageMap
1515
thresholds: Record<Threshold, number | undefined>
16+
perFile?: boolean
1617
configurationFile?: string
1718
}) {
1819
// Thresholds cannot be updated if there is no configuration file and
1920
// feature was enabled by CLI, e.g. --coverage.thresholdAutoUpdate
2021
if (!configurationFile)
2122
throw new Error('Missing configurationFile. The "coverage.thresholdAutoUpdate" can only be enabled when configuration file is used.')
2223

23-
const summary = coverageMap.getCoverageSummary()
24-
const thresholdsToUpdate: Threshold[] = []
24+
const summaries = perFile
25+
? coverageMap.files()
26+
.map((file: string) => coverageMap.fileCoverageFor(file).toSummary())
27+
: [coverageMap.getCoverageSummary()]
28+
29+
const thresholdsToUpdate: [Threshold, number][] = []
2530

2631
for (const key of THRESHOLD_KEYS) {
2732
const threshold = thresholds[key] || 100
28-
const actual = summary[key].pct
33+
const actual = Math.min(...summaries.map(summary => summary[key].pct))
2934

3035
if (actual > threshold)
31-
thresholdsToUpdate.push(key)
36+
thresholdsToUpdate.push([key, actual])
3237
}
3338

3439
if (thresholdsToUpdate.length === 0)
@@ -37,14 +42,14 @@ export class BaseCoverageProvider {
3742
const originalConfig = readFileSync(configurationFile, 'utf8')
3843
let updatedConfig = originalConfig
3944

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

4651
if (matches)
47-
updatedConfig = updatedConfig.replace(matches[0], matches[1] + summary[threshold].pct)
52+
updatedConfig = updatedConfig.replace(matches[0], matches[1] + newValue)
4853
else
4954
console.error(`Unable to update coverage threshold ${threshold}. No threshold found using pattern ${pattern}`)
5055
}

0 commit comments

Comments
 (0)
Please sign in to comment.