-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(coverage): automatic threshold updating (#2886)
Closes #1241
- Loading branch information
1 parent
615e150
commit e165216
Showing
19 changed files
with
203 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ const external = [ | |
'vitest', | ||
'vitest/node', | ||
'vitest/config', | ||
'vitest/coverage', | ||
] | ||
|
||
const plugins = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ const external = [ | |
'vitest', | ||
'vitest/node', | ||
'vitest/config', | ||
'vitest/coverage', | ||
] | ||
|
||
const plugins = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './dist/coverage.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { BaseCoverageProvider } from './utils/coverage' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { readFileSync, writeFileSync } from 'node:fs' | ||
import type { CoverageMap } from 'istanbul-lib-coverage' | ||
import type { BaseCoverageOptions, ResolvedCoverageOptions } from '../types' | ||
|
||
type Threshold = 'lines' | 'functions' | 'statements' | 'branches' | ||
|
||
const THRESHOLD_KEYS: Readonly<Threshold[]> = ['lines', 'functions', 'statements', 'branches'] | ||
|
||
export class BaseCoverageProvider { | ||
/** | ||
* Check if current coverage is above configured thresholds and bump the thresholds if needed | ||
*/ | ||
updateThresholds({ configurationFile, coverageMap, thresholds }: { | ||
coverageMap: CoverageMap | ||
thresholds: Record<Threshold, number | undefined> | ||
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[] = [] | ||
|
||
for (const key of THRESHOLD_KEYS) { | ||
const threshold = thresholds[key] || 100 | ||
const actual = summary[key].pct | ||
|
||
if (actual > threshold) | ||
thresholdsToUpdate.push(key) | ||
} | ||
|
||
if (thresholdsToUpdate.length === 0) | ||
return | ||
|
||
const originalConfig = readFileSync(configurationFile, 'utf8') | ||
let updatedConfig = originalConfig | ||
|
||
for (const threshold 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) | ||
else | ||
console.error(`Unable to update coverage threshold ${threshold}. No threshold found using pattern ${pattern}`) | ||
} | ||
|
||
if (updatedConfig !== originalConfig) { | ||
// eslint-disable-next-line no-console | ||
console.log('Updating thresholds to configuration file. You may want to push with updated coverage thresholds.') | ||
writeFileSync(configurationFile, updatedConfig, 'utf-8') | ||
} | ||
} | ||
|
||
/** | ||
* Resolve reporters from various configuration options | ||
*/ | ||
resolveReporters(configReporters: NonNullable<BaseCoverageOptions['reporter']>): ResolvedCoverageOptions['reporter'] { | ||
// E.g. { reporter: "html" } | ||
if (!Array.isArray(configReporters)) | ||
return [[configReporters, {}]] | ||
|
||
const resolvedReporters: ResolvedCoverageOptions['reporter'] = [] | ||
|
||
for (const reporter of configReporters) { | ||
if (Array.isArray(reporter)) { | ||
// E.g. { reporter: [ ["html", { skipEmpty: true }], ["lcov"], ["json", { file: "map.json" }] ]} | ||
resolvedReporters.push([reporter[0], reporter[1] || {}]) | ||
} | ||
else { | ||
// E.g. { reporter: ["html", "json"]} | ||
resolvedReporters.push([reporter, {}]) | ||
} | ||
} | ||
|
||
return resolvedReporters | ||
} | ||
} |
Oops, something went wrong.