-
Notifications
You must be signed in to change notification settings - Fork 18
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
Using import:
to mock globals messes up coverage (with c8)
#296
Comments
if there's no objection, I'd like to close this issue to merge with an earlier similar issue #271 (will wait for reply) There is a long-outstanding issue at the c8 repo for this bcoe/c8#325 The maintainer requested a reproduction and I submitted one, but there was no follow-up bcoe/c8#501 possibly the solution would need to come in multiple separated PRs |
I'm surprised the coverage report is correct when modules and not globals are mocked |
I have no problem with merging this with #271 but it would be nice to describe this To me it sounds like that these are 2 different problems that might have the same underlying cause? |
If you recommend any changes the title or anything else around that ticket they can be made.
Okay, keep this issue open For mocking global values, esmock returns a modified version of source file(s) to node's loader; where the global value being mocked is defined at the top of the file. Maybe that change would need to be coordinated with the coverage tool in some way, but to be honest I don't know how to solve the issue. const {fetch} = global.esmockCacheGet("treeid5"); // <- esmock adds this
// rest of file Any ideas for a solution? |
One thing I've been doing as an alternative way to mock these globals is to use For example something like this if I want to replace the global
You can make these changes to globalThis after the source file being tested is loaded, so you could put something like this in the logic of |
So it seems supporting Maybe support for a second "import"-like namespace, such as "globalThis", could be added in order to use One of the main benefits of esmock imo is that it can be used with concurrent tests and so using globalThis is a downgrade in that regard. If you are around feel free to chime in @koshic Also "setting" and "clearing" properties of |
I wish there were a way to avoid using |
If I use
import:
in esmock to mock a global it messes up the coverage report. At least it does when run withc8 mocha
. I constructed a minimal test scenario for it below:Here's an example source file
src/myfile.js
:And here's an example test for it. It only tests the
testFun
method, but it has a mock defined for the globalfetch
(which isn't actually used):When I run this and look at the coverage report it looks like this:
The above coverage report is clearly wrong.
If I comment out the line with the import, e.g.:
the coverage report is correct again:
Note that using esmock with things other than globals, doesn't have this issue, e.g. the following doesn't cause this issue:
It would be great if I can use the globals mocking without messing up the coverage report 😄
The text was updated successfully, but these errors were encountered: