fix(jest-runtime): correctly report V8 coverage with resetModules: true
#12912
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #12904
Summary
Bumped into the same issue yesterday in one of my projects. Using
resetModules: true
option or callingjest.resetModules()
breaks V8 coverage. Jest reports zero coverage for modules which were reset.Problem
Seems like the root of the issue is this line: https://github.com/facebook/jest/blob/9806c906f32b91868b4a5712f962a96484bcdf8c/packages/jest-runtime/src/index.ts#L1158
Calling
resetModules()
clears offthis._fileTransforms
map which is needed later in V8 coverage logic (to detect if a file was executed and to map coverage to sources as well):https://github.com/facebook/jest/blob/9806c906f32b91868b4a5712f962a96484bcdf8c/packages/jest-runtime/src/index.ts#L1206-L1217
Solution
Perhaps keeping a copy of sources for V8 in
this._v8CoverageSources
is the most optimal?It is also possible to collect coverage results with each
resetModule
call. Not sure if that is good idea, becauseresetModule
does not interrupt V8 anyhow.Test plan
An e2e test, which is failing on
main
is added.