jest icon indicating copy to clipboard operation
jest copied to clipboard

Feature request: CLI: Unset 'collectCoverageFrom' to default

Open quicksnap opened this issue 5 years ago • 7 comments

🚀 Feature Proposal

Continuing from #2831, I would like to have the ability to unset collectCoverageFrom so that it works like it is undefined

Motivation

In package.json, we have collectCoverageFrom set so that we generate coverage against files that are not touched by tests. However, when running a subset of tests locally, I would like the default behavior of only seeing coverage by affected files.

Currently, I must comment out the respective lines in package.json to leverage this behavior.

I've tried the following flags:

--collectCoverageFrom
--collectCoverageFrom 'undefined'
--collectCoverageFrom '[]'

Example

Perhaps an empty argument of --collectCoverageFrom would use undefined behavior?

quicksnap avatar Jun 28 '19 22:06 quicksnap

Looks like this line here filters out undefined: https://github.com/facebook/jest/blob/master/packages/jest-config/src/setFromArgv.ts#L18

One option would be to allow undefined for whitelisted keys?

quicksnap avatar Jun 28 '19 23:06 quicksnap

Would a PR along the lines of this be acceptable?

const undefinedWhitelist = ['coverage'];

// ...snip

  const argvToOptions = Object.keys(argv)
    .filter(
      key =>
        (argv[key] !== undefined || undefinedWhitelist.includes(key)) &&
        specialArgs.indexOf(key) === -1,
    )
    .reduce((options: Record<string, unknown>, key) => {

quicksnap avatar Jun 28 '19 23:06 quicksnap

bah, I suppose that would break it for anyone who doesn't explicitly provide --coverageFrom option, then..

quicksnap avatar Jun 28 '19 23:06 quicksnap

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Feb 25 '22 12:02 github-actions[bot]

Unstale

quicksnap avatar Feb 25 '22 17:02 quicksnap

we should support overriding if empty string is provided. wanna send a PR?

SimenB avatar Feb 26 '22 16:02 SimenB

I was trying to get the same behavior today (i.e. reset collectCoverageFrom to the default empty state). I believe this does in fact work today without any changes by using:

npx jest --collectCoverageFrom='[]' --coverageThreshold='{}'

Although coverageThreshold was not in the original request, that might be a common enough requirement to reset in this same situation that its worth noting.

The main difference between this syntax and what @quicksnap tried originally is the placement of the = after the flag. Without the = this does not work.

Run without any flags image

Run with flags image

In both runs it collects coverage for both tested and untestedUsed because tested imports from untestedUsed. In only the first run does it add untestedUnused to the coverage report and also triggers a coverage failure. In the second run with the flags set it will not add the modules which weren't in the test's code path (i.e. untestedUnused) and also isn't triggering a coverage failure.

shawnmcknight avatar Jan 22 '23 20:01 shawnmcknight

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jan 22 '24 21:01 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Feb 21 '24 21:02 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Mar 23 '24 00:03 github-actions[bot]