-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
- Loading branch information
1 parent
b67a5fb
commit 0f64076
Showing
13 changed files
with
140 additions
and
1 deletion.
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
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
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,55 @@ | ||
import { readFileSync, writeFileSync } from 'node:fs' | ||
import type { CoverageMap } from 'istanbul-lib-coverage' | ||
|
||
type Threshold = 'lines' | 'functions' | 'statements' | 'branches' | ||
|
||
const THRESHOLD_KEYS: Readonly<Threshold[]> = ['lines', 'functions', 'statements', 'branches'] | ||
|
||
/** | ||
* Check if current coverage is above configured thresholds and bump the thresholds if needed | ||
*/ | ||
export function 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') | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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