Doesn't seem to mock global by default
Closed this issue · 7 comments
Trying this out and i've found that jest is always resolving fetch-mock to the CJS server entry, and not the browser one, which results in the global fetch not being polyfilled. Is there a config i'm missing?
Oops spoke to soon, this doesn't work, because the browser field points to an mjs
file, which jest can't handle
Hey - first real user issue raised. Welcome 😄
Have you tried any of the approaches mentioned here jestjs/jest#4637. It does feel more like a jest issue to workaround than a problem with this library (or with fetch-mock either)
Yeah so i realized that i was complicating it by failing to correctly setup fetch-mock with a polyfill first, but yeah, this is ultimately probably a problem with jest not handling mjs files. That said, since this is specifically a jest extension, it probably makes more sense to require the cjs
file directly here instead of fetch-mock
and hoping Jest does the right thing?
here is ultimately the jest config that got this working:
},
"jest": {
"testEnvironment": "jsdom",
"moduleNameMapper": {
"fetch-mock$": "<rootDir>/node_modules/fetch-mock/cjs/client.js"
},
"setupFiles": [
"whatwg-fetch"
]
},
Yeah, that's a fair point. I didn't pay much attention to which file was being required, but am surprised that Jest - being the hippest test runner around - doesn't play nice with .mjs
I'll need to restructure this repo to have different server.js and browser.js exports in that case. No time to do so tonight, but maybe later this week.
@jquense I'd be grateful if you could try out v1.1.0-beta.2 to see if it works even when you remove the extra config you passed to jest
Please try v1.2.0 of this library and reopen the issue if it's still broken