Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: collect coverage from vm script #13962

Closed
wants to merge 7 commits into from
Closed

Conversation

GP4cK
Copy link
Contributor

@GP4cK GP4cK commented Feb 28, 2023

Summary

I'm basically continuing the work of: #10657.
If you think it's ok, I'll update the CHANGELOG.md.

Closes #9349

Test plan

I extend v8Coverage.test and compare if the code coverage report goes 100% in each columns as expection.

@GP4cK
Copy link
Contributor Author

GP4cK commented Feb 28, 2023

The CI is failing because it's adding coverage on cssTransform.js. Is it ok to update the snapshot? Or should it not be part of the coverage?

@SimenB
Copy link
Member

SimenB commented Feb 28, 2023

cssTransform.js should not be included as it's not part of the tests - it's part of the Jest config. This is what the condition you've removed is supposed to guard against (only check coverage of files part of the test run, not other files). So I think we need to figure out something more clever, not remove it altogether.

@GP4cK
Copy link
Contributor Author

GP4cK commented Feb 28, 2023

I see, thanks for the explanation.
Then how about leaving the line this._v8CoverageSources!.has(res.url), but if there is a collectCoverageFrom set, then it supersedes it?
This wouldn't fix the issue entirely but at least it should give a workaround for users who need it.

@SimenB
Copy link
Member

SimenB commented Feb 28, 2023

Yeah, that sounds reasonable! Might give false positives if file is also pulled in by jest itself, but probably edge casey enough

@SimenB
Copy link
Member

SimenB commented Feb 28, 2023

Seems ci is not totally happy

@GP4cK
Copy link
Contributor Author

GP4cK commented Mar 1, 2023

I made the change that we agreed on. The CI should be good now. Please let me know if there's anything else.

return this._v8CoverageResult
.filter(res => res.url.startsWith('file://'))
.map(res => ({...res, url: fileURLToPath(res.url)}))
.filter(
res =>
// TODO: will this work on windows? It might be better if `shouldInstrument` deals with it anyways
res.url.startsWith(this._config.rootDir) &&
this._v8CoverageSources!.has(res.url) &&
matchesCoveragePattern(res.url) &&
shouldInstrument(res.url, this._coverageOptions, this._config),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, thinking about it - shouldn't this option be enough? and the fact the css transform is included is because we are missing an ignore pattern in our test?

(sorry about the back and forth 🙈)

Copy link
Contributor Author

@GP4cK GP4cK Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries at all. I'm really not familiar with the code base or v8 so I don't fully understand what's going on.
I was simply following the instructions from someone who answered my question on StackOverflow.

I blamed the file and I see that _v8CoverageSources was introduced as a substitute to _fileTransforms in #12912 to avoid losing the list of files when calling jest.resetModules().

However, I don't see why we would need this in the filter. The 2 other checks should be enough:

res.url.startsWith(this._config.rootDir) && shouldInstrument(...);

This is why this PR was removing it: #10657

So if you agree, I will

  1. remove this line
  2. exclude cssTransform.js in the other test with coveragePathIgnorePatterns so that it doesn't appear in the coverage.

Could you confirm this is the correct way?

Edit: The problem is if I do that, then this line here may break and I don't know where I could get the transformedFile

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it looks right if by default only files explicitly imported in tests are included in the coverage report. For example, that’s the default of c8. They have all, include, exclude option to include other files.

So it made sense that collectCoverageFrom option is collecting coverage from not only imported files. Current Jest documentation of the option agrees: "If a file matches the specified glob pattern, coverage information will be collected for it even if no tests exist for this file and it's never required in the test suite."

If I didn't miss some detail.

Copy link
Contributor Author

@GP4cK GP4cK Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe if I can't find the file in _v8CoverageSource, I can call transformFile() on it and add it to the map. Would that be ok?

... or it's ok to have codeTransformResult = undefined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you have in mind. I was trying to say that not including cssTransform.js in coverage report by default seems to be expected behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrazauskas so do you mean we shouldn't touch that part of the code and find another way to add the files loaded with vm.runInContext to _v8CoverageSources?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s wait for Simen’s feedback. He was commenting that cssTransform.js could get included or not because of ignore pattern. I try to suggest that by default only explicitly imported files should be included in the report and collectCoverageFrom option could be used to override that. This is what your current implementation does. If I got it right.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought was that shouldInstrument should return the right thing - if a file shouldn't get instrumented, it shouldn't be include in a coverage report

Copy link
Contributor Author

@GP4cK GP4cK Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimenB you're right, my change didn't make sense.
I've removed the line this._v8CoverageSources!.has(res.url) so we only rely on shouldInstrument and added cssTransform.js to the coveragePathIgnorePatterns in the other tests. I hope that's ok.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this seems more reasonable to me 👍

Comment on lines +10 to +12
"coveragePathIgnorePatterns": [
"cssTransform\\.js"
]
Copy link
Contributor

@mrazauskas mrazauskas Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks breaking for me, because it is only needed with V8 provider. Wouldn’t it be better to keep default behaviour similar between Babel and V8 providers? See alternative proposal in #13974

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good with your proposal. Thanks!

@SimenB SimenB closed this in #13974 Mar 4, 2023
@GP4cK GP4cK deleted the vmscript-coverage branch March 4, 2023 23:52
@github-actions
Copy link

github-actions bot commented Apr 4, 2023

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No coverage reports for files that are executed by vm.runInContext.
4 participants