Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(coverage): thresholdAutoUpdate to work with perFile #3182

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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