Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Doesn't seem to mock global by default #4

Closed
jquense opened this issue Jan 7, 2020 · 7 comments
Closed

Doesn't seem to mock global by default #4

jquense opened this issue Jan 7, 2020 · 7 comments
Labels
enhancement New feature or request

Comments

@jquense
Copy link

jquense commented Jan 7, 2020

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?

@jquense
Copy link
Author

jquense commented Jan 7, 2020

@jquense jquense closed this as completed Jan 7, 2020
@jquense jquense reopened this Jan 7, 2020
@jquense
Copy link
Author

jquense commented Jan 7, 2020

Oops spoke to soon, this doesn't work, because the browser field points to an mjs file, which jest can't handle

@wheresrhys
Copy link
Owner

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)

@jquense
Copy link
Author

jquense commented Jan 13, 2020

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"
    ]
  },

@wheresrhys
Copy link
Owner

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.

@wheresrhys wheresrhys added the enhancement New feature or request label Jan 13, 2020
@wheresrhys
Copy link
Owner

wheresrhys commented Jan 19, 2020

@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

@wheresrhys
Copy link
Owner

Please try v1.2.0 of this library and reopen the issue if it's still broken

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants