-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat(coverage): automatic threshold updating #2886
Merged
sheremet-va
merged 2 commits into
vitest-dev:main
from
AriPerkkio:feat/coverage-threshold-update
Feb 25, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vitest/coverage
entry point should also be added totsconfig
asvitest/src/coverage
otherwise it includesdist
folderThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the root
tsconfig.json
. Though I'm not exactly sure why that was needed, as the typings were already working intest/coverage-test
and locally linked project.