Skip to content

Commit

Permalink
fix(coverage): thresholdAutoUpdate to work with perFile (#3182)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Apr 12, 2023
1 parent baf902a commit 29eebf6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/coverage-c8/src/provider.ts
Expand Up @@ -187,6 +187,7 @@ export class C8CoverageProvider extends BaseCoverageProvider implements Coverage
lines: this.options.lines,
statements: this.options.statements,
},
perFile: this.options.perFile,
configurationFile: this.ctx.server.config.configFile,
})
}
Expand Down
1 change: 1 addition & 0 deletions packages/coverage-istanbul/src/provider.ts
Expand Up @@ -164,6 +164,7 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
lines: this.options.lines,
statements: this.options.statements,
},
perFile: this.options.perFile,
configurationFile: this.ctx.server.config.configFile,
})
}
Expand Down
19 changes: 12 additions & 7 deletions packages/vitest/src/utils/coverage.ts
Expand Up @@ -10,25 +10,30 @@ export class BaseCoverageProvider {
/**
* Check if current coverage is above configured thresholds and bump the thresholds if needed
*/
updateThresholds({ configurationFile, coverageMap, thresholds }: {
updateThresholds({ configurationFile, coverageMap, thresholds, perFile }: {
coverageMap: CoverageMap
thresholds: Record<Threshold, number | undefined>
perFile?: boolean
configurationFile?: string
}) {
// Thresholds cannot be updated if there is no configuration file and
// feature was enabled by CLI, e.g. --coverage.thresholdAutoUpdate
if (!configurationFile)
throw new Error('Missing configurationFile. The "coverage.thresholdAutoUpdate" can only be enabled when configuration file is used.')

const summary = coverageMap.getCoverageSummary()
const thresholdsToUpdate: Threshold[] = []
const summaries = perFile
? coverageMap.files()
.map((file: string) => coverageMap.fileCoverageFor(file).toSummary())
: [coverageMap.getCoverageSummary()]

const thresholdsToUpdate: [Threshold, number][] = []

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

if (actual > threshold)
thresholdsToUpdate.push(key)
thresholdsToUpdate.push([key, actual])
}

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

for (const threshold of thresholdsToUpdate) {
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 pattern = new RegExp(`(${threshold}\\s*:\\s*)${previousThreshold.replace('.', '\\.')}`)
const matches = originalConfig.match(pattern)

if (matches)
updatedConfig = updatedConfig.replace(matches[0], matches[1] + summary[threshold].pct)
updatedConfig = updatedConfig.replace(matches[0], matches[1] + newValue)
else
console.error(`Unable to update coverage threshold ${threshold}. No threshold found using pattern ${pattern}`)
}
Expand Down

0 comments on commit 29eebf6

Please sign in to comment.