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.

Code coverage is missing when using projects, if each project's rootDir is not set to the root rootDir's value

See original GitHub issue

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

A bug.

What is the current behavior?

When you’re using Jest with projects and you want to collect coverage across them, following this advice https://github.com/facebook/jest/issues/4255#issuecomment-321939025 does not give you coverage. You still need to set each project’s rootDir setting to the root root, e.g. "../" (https://github.com/adaniliuk/jest-projects-config-issue/blob/master/client/jest.config.js#L4).

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.

You can see it here. https://github.com/jure/jest-projects-config-issue. The current result is:

Test Suites: 2 passed, 2 total
Tests:       4 passed, 4 total
Snapshots:   1 passed, 1 total
Time:        1.809s, estimated 2s
Ran all test suites in 2 projects.
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |  Unknown |  Unknown |  Unknown |  Unknown |                |
----------|----------|----------|----------|----------|----------------|

If you were to add rootDir ../../ to each project, and adjust the configs accordingly, it would work OK.

What is the expected behavior?

Configuring a global coverage config (e.g. here https://github.com/jure/jest-projects-config-issue/blob/master/jest.config.js#L6) should use the global rootDir (which is also the context of the config at that point), when matching files for instrumentation here: https://github.com/facebook/jest/blob/9afeb9c757e4b92dbf670e66545dce3df5c407c4/packages/jest-runtime/src/should_instrument.js#L59-L69.

As it is, the match is done against the relative path, where rootDir is the rootDir of each project, e.g.

relative filename:  server.js
filename:  /Users/juretriglav/src/config-issue-2/packages/server/server.js
rootDir:  /Users/juretriglav/src/config-issue-2/packages/server

This means that, for example, the global pattern 'packages/server/**/*.{js,jsx}' won’t match.

Possible workarounds

  1. It’s possible to specify global coverage patterns in their relative form, e.g. '*.js', but those are too generic and match too much compared to their specific alternatives.

  2. As mentioned above, one can specify rootDir to be the global rootDir in each package, and adjust all config accordingly. This seems like more of a hack than a workable solution.

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

Jest 22.1.4, Node.js 9.4.0, macOS 10.12.6

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:16
  • Comments:7

github_iconTop GitHub Comments

2reactions
eric-burelcommented, Feb 3, 2021

I feel like there is something off with collectCoverageFrom and testMatch options. It seems to exclude tests automatically based on testMatch option, but when using a project, since you have multiple testMatch, tests are not removed from my test. It doesn’t manage to “merge” all patterns.

0reactions
Yegorich555commented, Nov 11, 2022

Any changes ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
This will collect coverage information for all the files inside the project's rootDir , except the ones that match **/node_modules/** or **/vendor/** ....
Read more >
error TS6059: File is not under 'rootDir' .. 'rootDir' is expected ...
rootDir is set to a root folder, that contains all your source files. ... If it cannot find one in notifier-server project root...
Read more >
TSConfig Reference - Docs on every TSConfig option
A TSConfig file in a directory indicates that the directory is the root of a TypeScript or JavaScript project... Compiler Options. Top Level....
Read more >
Typescript — How to solve the problem with unresolved path ...
The rootDir is the /src subfolder and contains all of our source files. The beseUrl is the project folder -where the tsconfig.json is....
Read more >
Config Files - Babel.js
For project-wide configuration, Babel will automatically search for a ... painful to use in monorepos if the working directory is not the monorepo...
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