-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Description
🚀 Feature Proposal
Allow the code coverage to be collected for files outside the configured rootDir
when using the V8 as coverage provider.
Motivation
Currently, Jest artificially excludes files outside of rootDir
from V8 code coverage. This restriction is implemented in a single line that prevents V8 from instrumenting files outside rootDir
:
jest/packages/jest-runtime/src/index.ts
Line 1345 in 7998905
res.url.startsWith(this._config.rootDir) && |
At the same time, V8 is fully capable of instrumenting and collecting coverage for all executed files. If V8 supports that, I don't see a reason why Jest shouldn't allow users to benefit from that. I experienced at least two problematic scenarios, due to this Jest's limitation:
-
Running Jest from subdirectories - When running Jest from outside a package's root (e.g., from a
tests
folder), coverage is not collected for the package's source code in parallel directories likesrc
. Similar issues have been reported in [Bug]: Running jest from sibling directory to source code prevents code coverage with istanbul #12227 and [Feature]: Allow source files outside of rootDir to be found by collectCoverageFrom #13994, though has never brought any attention. -
Code coverage for integration tests - In the monorepo I am currently working with, we have both unit and integration tests. Integration tests execute scattered around code different packages from the repository, while its
rootDir
is set to directory designated for tests. With such setup it impossible to collect coverage for all packages affected by integration tests. Technically, this could be solved by movingrootDir
to the root of the repository, assuming hoisting is used. However, that would not work in case of PNPM-like dependency management, where hoisting is not present, like in our case.
The same issue occurs for coverage using Babel, but there the decision to skip files outside of rootDir
is made on Istanbul side through their babel-plugin-istanbul
and test-exclude
packages, right here: https://github.com/istanbuljs/babel-plugin-istanbul/blob/master/src/index.js#L82-L89. That's why I am limiting this feature request just to V8 for now.
Example
A reproduction repository demonstrating the issue is available at: https://github.com/kwaszczuk/jest-cov-repro
Pitch
As mentioned in the Motivation section, the exclusion of files outside of rootDir
is happening within jest-runtime
:
jest/packages/jest-runtime/src/index.ts
Line 1345 in 7998905
res.url.startsWith(this._config.rootDir) && |
At the same time, I believe addressing this matter doesn't require much effort, yet provides a better Jest usability and configurability in some edge cases. Perhaps, it could be allowed to omit the aforementioned file exclusion by setting an new option like collectCoverageOutsideRoot
to remain backward-compatible.