jest: Wrong coverage values for files outside of (but still detected)

Do you want to request a feature or report a bug?

bug

What is the current behavior?

When I run code coverage on code inside <rootDir>, everything works like a charm, but for code outside of <rootDir>, the files are detected but coverage values are wrong. E.g. capture

Note: The sums or average on the All files line seem to be wrong too. E.g. Branch coverage at 0 when some of the values are at 100. Note2: At least the files being outside of <rootDir> seems to be the problem.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

I have reproduced the issue on this repo: https://github.com/Zephir77167/jest-issues (sorry I’ve never used yarn so I set it up with npm)

What is the expected behavior?

I would expext the coverage values for files outside of <rootDir> to be correct

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Jest config:

{
  "collectCoverageFrom": [
    "client/src/utils/*.js",
    "../shared/utils/*.js"
  ],
  "coverageDirectory": "<rootDir>/coverage",
  "coverageThreshold": {
    "global": {
      "branches": 0,
      "functions": 0,
      "lines": 0,
      "statements": 0
    }
  },
  "moduleFileExtensions": [
    "js"
  ],
  "moduleDirectories": [
    "node_modules",
    "project1/node_modules"
  ],
  "modulePaths": [
    "<rootDir>/client"
  ],
  "roots": [
    "<rootDir>",
    "<rootDir>/../shared"
  ],
  "testRegex": "\\.test\\.js$"
}

Jest-cli version: 19.0.0 Node: 5.6.0 NPM: 3.6.0 OS: Windows 10

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 16 (1 by maintainers)

Most upvoted comments

You shouldn’t look outside your rootDir, change your config to something like this:

roots: [
  "<rootDir>/myRootDir",
  "<rootDir>/shared"
]

The current configuration of my project forces me to have my folder project1 with all my node_modules in it, and a shared folder alongside. I have other folders project2, project3, etc which all rely on shared, but I can’t just share the node_modules between all the projects.

This config works well with Jest and the rest of the JS stack (webpack/eslint/…), my only problem is on the coverage which just shows wrong values.

“I shouldn’t look outside rootDir”: here I have no choice, I can’t just change the architecture of all my project to make coverage work, as all the rest works perfectly fine for now.

PS: furthermore, files are detected and appear in the coverage, so I don’t really see why their location is a problem

Another one impacted by this here.

@cpojer Totally understand the by design aspect of it, but can we look at this as a feature request rether than a defect and have it reopened?

Thanks heaps.

Please reopen this issue.

Please re-open this issue!!

Same issue here… Tried very long time to find the issue until seeing this… please help to fix it. Thanks!

@cpojer Do we have any solution for above issue? I have exactly same hierarchy as explained by @Zephir77167 in previous message. Files outside rootDir are being detected using relative path and even tests are running for same. So seems config works for me. Only issue is that, coverage is generated 0% for all of them…

PS: it shows 100% coverage for empty files while 0% for files with some code.

having exactly the same issue. if there is no plan or intention to handle files outside rootDir, it would be very useful to at least issue a warning in the logs