Comments (7)
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
from esmock.
I'm surprised the coverage report is correct when modules and not globals are mocked
from esmock.
I have no problem with merging this with #271 but it would be nice to describe this import:
related problem there then too. I did read that issue before reporting this one and thought it was about a different problem.
To me it sounds like that these are 2 different problems that might have the same underlying cause?
from esmock.
If you recommend any changes the title or anything else around that ticket they can be made.
To me it sounds like that these are 2 different problems that might have the same underlying cause?
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?
from esmock.
Any ideas for a solution?
One thing I've been doing as an alternative way to mock these globals is to use globalThis
.
For example something like this if I want to replace the global fetch
:
const mockFetch = async (url) => { ... }; // My mock fetch function
const savedFetch = globalThis.fetch; // Save the original global fetch
try {
globalThis.fetch = mockFetch; // replace the global fetch with my mockFetch
// Now call my function that calls fetch under the covers
// it will use the mockFetch
assert.strictEqual(418, await testFun());
} finally {
globalThis.fetch = savedFetch; // put the original global fetch back in the finally block
}
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 await esmock(...)
call without the need to alter the original source file.
The only thing that needs to be done extra is to reset the original values, which I do in a finally
block. Maybe an extra call something like esmock.resetGlobals()
could do this, but there might be a smarter way to do that somehow.
from esmock.
So it seems supporting globalThis
and esmock.resetGlobals()
would require esmock to lose its ability to be used concurrently OR esmock would need to provide an extra interface and logic that conditionally use globalThis
and implicitly requires sequential usage from the user.
Maybe support for a second "import"-like namespace, such as "globalThis", could be added in order to use globalThis
for mocking.
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 globalThis
could be handled with a seperate file or package and does not actually need to be handled by esmock. However, for users who would want to use globalThis
, it would be convenient if esmock could help with that.
from esmock.
I wish there were a way to avoid using globalThis
and to get correct results from c8 here but don't know if, or how, it could be achieved.
from esmock.
Related Issues (20)
- yarn pnp issues HOT 14
- Is it possible to mock json imports with assert? HOT 6
- Support Yarn PnP resolver out of the box HOT 11
- Get rid of old Node versions support HOT 8
- resolve error introduced with 2.5.2 HOT 10
- Error when mocking a file that exports an array. HOT 1
- c8 incorrect branch coverage; modules and `import` globals HOT 11
- how to mock globalThis HOT 4
- import('node:fs/promises') called within a library does not seem to be replaced HOT 30
- Path alias support HOT 1
- Unexpected strict mode reserved word syntax error when mocking express HOT 2
- incompatible with eslint-plugin-import HOT 3
- No support for subpath patterns? HOT 14
- Version 2.6.3 changed the way to mock libraries HOT 7
- ESMock seems to wrongfully alter Sinon stubs. HOT 1
- [Question] Is it possible to mock indirect imports? HOT 10
- Scoped mocks failing with pnpm but working with npm HOT 9
- esmock static import for classes not matching HOT 3
- upstream issue, worker threads unable to register loader
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esmock.