jest: Jest is "stuck" on only generating coverage for tested files, instead of all files

šŸ› Bug Report

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.

To Reproduce

I had a config with the following (this worked fine): Screen Shot 2019-04-11 at 12 15 04 PM

with a command that looked like this: Screen Shot 2019-04-11 at 12 15 47 PM

I removed the collectCoverage and collectCoverageFrom options from the jest config, and created a separate command that looked like this: Screen Shot 2019-04-11 at 12 17 12 PM

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.

Expected behavior

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.

Run npx envinfo --preset jest

Paste 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

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 19

Most upvoted comments

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.

@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"
    ]
  }

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.

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.

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.

@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

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