Skip to content

[Feature]: Allow V8 coverage collection for files outside rootDir #15715

@kwaszczuk

Description

@kwaszczuk

🚀 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:

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:

  1. 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 like src. 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.

  2. 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 moving rootDir 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:

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions