I don't get the same coverage report depending of the location of my jest config.json.
I made a repository to reproduce the issue https://github.com/Edistra/jest_coverage_report_issue
I've just written a simple function file and its associated test in src folder.
My jest.config.json, at the root of my project, looks like
{
"roots": [
"src",
"node_modules"
],
"collectCoverage": true,
"collectCoverageFrom": [
"src/**.js"
]
}
I've got another one in a config folder with the appropriate paths
{
"roots": [
"../src",
"../node_modules"
],
"collectCoverage": true,
"collectCoverageFrom": [
"../src/**.js"
]
}
My npm scripts are simple
"scripts": {
"test": "jest --config jest.config.json",
"test-issue": "jest --config config/jest.config.json"
}
The first one generates a correct report

The second one seems to consider that my function is not covered

This diff fixes it:
diff --git i/config/jest.config.json w/config/jest.config.json
index 7c9d500..6570050 100644
--- i/config/jest.config.json
+++ w/config/jest.config.json
@@ -1,10 +1,11 @@
{
+ "rootDir": "../",
"roots": [
- "../src",
- "../node_modules"
+ "src",
+ "node_modules"
],
"collectCoverage": true,
"collectCoverageFrom": [
- "../src/**.js"
+ "src/**.js"
]
}
Would probably be fixed by https://github.com/facebook/jest/pull/4587
That diff sort of fixes it but then you have to put <rootDir>/config/ everywhere in it instead of just<rootDir>/
Most helpful comment
This diff fixes it: