question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

šŸ› 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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19

github_iconTop GitHub Comments

13reactions
cyphernullcommented, Jul 12, 2020

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.

4reactions
ghostcommented, Oct 3, 2020

@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"
    ]
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest finds tests but doesn't collect coverage - Stack Overflow
The quick fix I said in my comment, using --watchAll instead, eg: react-scripts test --coverage --watchAll . Just for future reference,Ā ...
Read more >
Configuring code coverage in Jest, the right way
In this brief tutorial we see how to configure code coverage in Jest, the popular testing framework for JavaScript. If you're new to...
Read more >
Configuring Jest to show code coverage for all of your files
At first I was thinking the best bet would be to just stub out a test file for each file we wanted to...
Read more >
Configuring Jest
Indicates whether the coverage information should be collected while executing the test. Because this retrofits all executed files with coverage collectionĀ ...
Read more >
Coverage | Code Cookbook - Michael Currin
{ "scripts": { "cover:unit": "nyc --silent npm run test:unit", ... Jest is ā€œstuckā€ on only generating coverage for tested files, instead of all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found