Code coverage is missing when using projects, if each project's rootDir is not set to the root rootDir's value
See original GitHub issueDo 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
-
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. -
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:
- Created 6 years ago
- Reactions:16
- Comments:7
Top GitHub Comments
I feel like there is something off with
collectCoverageFrom
andtestMatch
options. It seems to exclude tests automatically based ontestMatch
option, but when using a project, since you have multipletestMatch
, tests are not removed from my test. It doesn’t manage to “merge” all patterns.Any changes ?