I recently setup codecov, and then setup jest to generate coverage reports for it.
Initially, it was setup to collect coverage from _all_ files via the jest config (see below).
Later, I wanted to have a separate jest command minus the coverage report, so I moved the collect coverage command from the jest options to the cli (see below).
After doing this, jest no longer gives coverage for the entire src folder, but only for the files tested. Okay, no problem, I'll just undo this change.
Here's where I am getting frustrated - no attempts to revert this seem to work. I can revert to the previous config, and it no longer works for _all_ files, only the ones being tested. Same config, different results. I've tried running it with --clearCache or --no-cache and neither work. I've tried creating a whole new config, one with coverage and one without, and then just running the base command, and that doesn't work either.
When running, it takes much longer than it used, displays the 'generating coverage for untested files' message, and then still only returns the coverage for tested files only.
I had a config with the following (this worked fine):

with a command that looked like this:

I removed the collectCoverage and collectCoverageFrom options from the jest config, and created a separate command that looked like this:

I realize now that the above command never worked for all files. Regardless, after reverting to the original setup, jest no longer works the way it used to, and is instead "stuck" on just reporting coverage for the tested files only. Even though it displays the "generating coverage for untested files" message.
I would expect that when coverage is set to true, and collectCoverage is passed a glob for all files in the repo, that the generated coverage report would be for all files. I would expect this behavior to be consistent.
npx envinfo --preset jestPaste the results here:
System:
OS: macOS 10.14.4
CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
Binaries:
Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/bin/npm
npmPackages:
jest: ~24.1.0 => 24.7.1
Could you provide a minimal reproduction we could download and investigate?
Coverage report for all files worked fine in v23 but seem like it has regressed in v24 - I'm only seeing coverage for tested files.
Apologies - this stripped down example repo shows it working as expected.
https://github.com/charliekassel/test-jest-coverage
@charliekassel huh, thanks for that comment. we recently updated to version 24. after downgrading to version 23, everything works again
gonna close this issue since my problem is resolved. but seems like something cache related possibly might have regressed in v24
I'm experiencing this too. Is there a workaround? I can even list file paths explicitly, and the file is not included in the coverage.
I had the same problem. But I find an exclude rule that not exclude the subdirectories :
collectCoverageFrom: [
'**/*.{js,jsx}',
'!dist/*',
]
So the coverage was ran on big JS files. I suppose it is too much for coverage calculation.
I change !dist/* to !dist/** to really exclude the files. And it worked
I am having the same/similar problem. I am passing --collectCoverageFrom 'src/**/*{js,jsx,ts,tsx}' and many files in src/ are being excluded from the total coverage.
@rwwagner90 Did you end up finding a solution to this?
@bigwoof91 I think we just opted for passing a lot more paths instead of one glob. See https://github.com/shipshapecode/shepherd/blob/master/jest.config.js#L11-L17
Appreciate the quick response @rwwagner90 . Tried passing more glob patterns. Unfortunately it didn't do the trick (using [email protected]) -- although, I'm happy it worked for you!
FWIW
We are running jest, specifying to test only certain files, then trying to collect coverage on the entire src directory. I've read through this issue and this related PR from way back in 2016... tried several things with no avail. My team might raise this issue again.
Any tips are welcome -- otherwise, thanks again!
@bigwoof91 I think you will have to do like we did and pass many different globs, instead of one for the whole directory. I could not get the one for the whole directory to work.
Hi everyone, Tom from Codecov here. I wanted to know if I could be useful here, but we recently released a feature called Carryforward Flags, which allows users to only upload coverage reports for code that changes.
Here are a bunch of resources that might help:
Blog
Documentation
Webinar
If this can be useful, I'd love to see how I can help. Please let me know.
Can this be fixed? It was first raised way back in 2016 as part of #1211, like @bigwoof91 mentioned, and people are still having this same issue.
Like...do you jest folks even get the point of coverage!? What's the point if I add new files to directories that I've included under collectCoverageFrom, but only have those that are touched by tests get included in coverage?
I can add a whole bunch of new files with no tests and get 100% coverage.
Hey guys, I think I find the answer.
I encountered with this issue. And I found my directory structure as:
路 __tests__/
路 src/
路 jest.config.js
The test files is listed outside of source files. However, my previous jest config's _roots_ property only had the __tests__/ directory. When I added the src/ directory to _roots_. Jest gave coverage of all wanted files in src/. I hope this would help.
@FunkyLambda
@cyphernull Thank for helping, following is my package.jsonconfig that worked
"jest": {
"collectCoverageFrom": [
"src/**/*.js"
],
"collectCoverage": true,
"verbose": true,
"testTimeout": 30000,
"roots": [
"<rootDir>/tests",
"<rootDir>/src"
],
"testRegex": "((\\.|/*.)(test))\\.js?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
@cyphernull Thank for helping, following is my package.jsonconfig that worked
"jest": { "collectCoverageFrom": [ "src/**/*.js" ], "collectCoverage": true, "verbose": true, "testTimeout": 30000, "roots": [ "<rootDir>/tests", "<rootDir>/src" ], "testRegex": "((\\.|/*.)(test))\\.js?$", "moduleFileExtensions": [ "ts", "tsx", "js", "jsx", "json", "node" ] }
Thank you! roots with two paths, move back my tests folder outside src and collectCoverageFrom without <rootDir> fix that "issue" for me
Most helpful comment
Hey guys, I think I find the answer.
I encountered with this issue. And I found my directory structure as:
路 __tests__/
路 src/
路 jest.config.js
The test files is listed outside of source files. However, my previous jest config's _roots_ property only had the __tests__/ directory. When I added the src/ directory to _roots_. Jest gave coverage of all wanted files in src/. I hope this would help.