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

Support 100 shorthand option for pattern based coverage rules #6166

Closed
4 tasks done
thor-juhasz opened this issue Jul 18, 2024 · 1 comment · Fixed by #6174
Closed
4 tasks done

Support 100 shorthand option for pattern based coverage rules #6166

thor-juhasz opened this issue Jul 18, 2024 · 1 comment · Fixed by #6174
Labels
feat: coverage Issues and PRs related to the coverage feature good first issue Good for newcomers p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@thor-juhasz
Copy link
Contributor

Clear and concise description of the problem

As documented here, we can use this:

coverage: {
    thresholds: {
        '100': true,
    },
}

This I find to be very useful instead of typing out all of the options with the same value.
This is however, not supported for pattern based thresholds, where, I would argue, it becomes even more useful.

Suggested solution

Allow '100': true in pattern based coverage tresholds:

coverage: {
    thresholds: {
        lines: 50,
        function: 70,

        'src/path1/**/*': { '100': true },
        'src/path2/**/*': { '100': true },
        'src/path3/**/*': { '100': true },
        'src/path4/**/*': { '100': true },
        'src/path5/**/*': { '100': true },
    },
}

This reduces clutter far more than only allowing this for the global threshold config.

Alternative

No response

Additional context

No response

Validations

@thor-juhasz thor-juhasz changed the title Support 100 coverage for pattern based coverage rules Support 100 shorthand option for pattern based coverage rules Jul 18, 2024
@AriPerkkio AriPerkkio added good first issue Good for newcomers feat: coverage Issues and PRs related to the coverage feature p2-nice-to-have Not breaking anything but nice to have (priority) and removed enhancement: pending triage labels Jul 18, 2024
@AriPerkkio
Copy link
Member

Related code is around here:

/**
* Constructs collected coverage and users' threshold options into separate sets
* where each threshold set holds their own coverage maps. Threshold set is either
* for specific files defined by glob pattern or global for all other files.
*/
resolveThresholds({
coverageMap,
thresholds,
createCoverageMap,
root,
}: {
coverageMap: CoverageMap
thresholds: NonNullable<BaseCoverageOptions['thresholds']>
createCoverageMap: () => CoverageMap
root: string
}): ResolvedThreshold[] {
const resolvedThresholds: ResolvedThreshold[] = []
const files = coverageMap.files()
const filesMatchedByGlobs: string[] = []
const globalCoverageMap = createCoverageMap()
for (const key of Object.keys(
thresholds,
) as `${keyof typeof thresholds}`[]) {
if (
key === 'perFile'
|| key === 'autoUpdate'
|| key === '100'
|| THRESHOLD_KEYS.includes(key)
) {
continue
}
const glob = key
const globThresholds = resolveGlobThresholds(thresholds[glob])
const globCoverageMap = createCoverageMap()
const matchingFiles = files.filter(file =>
mm.isMatch(relative(root, file), glob),
)
filesMatchedByGlobs.push(...matchingFiles)
for (const file of matchingFiles) {
const fileCoverage = coverageMap.fileCoverageFor(file)
globCoverageMap.addFileCoverage(fileCoverage)
}
resolvedThresholds.push({
name: glob,
coverageMap: globCoverageMap,
thresholds: globThresholds,
})
}
// Global threshold is for all files that were not included by glob patterns
for (const file of files.filter(
file => !filesMatchedByGlobs.includes(file),
)) {
const fileCoverage = coverageMap.fileCoverageFor(file)
globalCoverageMap.addFileCoverage(fileCoverage)
}
resolvedThresholds.unshift({
name: GLOBAL_THRESHOLDS_KEY,
coverageMap: globalCoverageMap,
thresholds: {
branches: thresholds.branches,
functions: thresholds.functions,
lines: thresholds.lines,
statements: thresholds.statements,
},
})
return resolvedThresholds
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: coverage Issues and PRs related to the coverage feature good first issue Good for newcomers p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
2 participants