-
-
Notifications
You must be signed in to change notification settings - Fork 483
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
ReferenceError: fetch is not defined #686
Comments
Hey @onimougwo, This is a configuration issue. You need to include a fetch polyfill (like |
The first thing to understand is that The solution to your issue is to include a suitable fetch polyfill in your testing setup (just as @msutkowski has pointed out). The other part is this question:
It'd be much more straightforward to understand and debug any potential MSW issues when you realize it's request client-agnostic. MSW doesn't have any fetch-related logic and doesn't rely on fetch in any way. Seeing errors like "fetch is not defined" may hint to you that it's not a library's problem. It's the core philosophy and one of the main benefits of MSW: you forget about mocking fetch/axios/etc. Departing from that mentally is also helpful to keep your tests clean and debugging easier. |
I've tried to import Not clear how to define fake I red all MSW documentation and all examples in repository and did not manage to find example how to test React components (which uses For example, let's look on this part of documentation "Getting Started -> Integrate -> Node" - https://mswjs.io/docs/getting-started/integrate/node - fetch not mentioned there. I thought that MSW provides some fake I red brilliant article https://kentcdodds.com/blog/stop-mocking-fetch with headline "Stop mocking fetch" and irony is that after I did step by step all code examples in this article I got "ReferenceError: fetch is not defined" - so to stop mocking fetch I do need to mock fetch? I'am completely lost. Do you have any open-source example how to test React components (which uses |
Hey, @pqr. I'm sorry to hear you're having trouble setting up your tests. You don't find In reality, your React component gets rendered in Node.js when you run Jest, and I've added a new usage example that will help you set up Jest and MSW: |
@kettanaito now I catch the idea! Thank you for complete example without CRA, it has really helped me to figure out the point of |
Setting up things is far from easy. I'm glad that there's at least one example of how you can do that. Hope it helps. |
Thank you so much @kettanaito |
@msutkowski I spent a whole day trying to set it up but no success so far. We have a couple of such tests. They use fetch-mock and node environment to not bring the overhead of jsdom. I want to migrate them to msw but can't figure out how Are you aware of any workarounds for this? Thanks in advance and sorry for posting in a closed issue |
Hey, @MikeYermolayev. Take a look at this Jest + MSW example that uses the most minimal Jest setup there can be. It uses |
@kettanaito yes, I've studied this example. The problem is that I've tried to put tests which use browser API in I guess the only options are:
As for now I simply switched to jsdom. There were not so many tests with mocks so it won't have a big impact on tests performance |
The fact that you mention
That's absolutely correct: nothing to be relative to in Node.js. Relative URLs exist in a browser, where they are relative to the current location. You'd never put a browser code in a Node.js testing environment. Perhaps what you're trying to test shouldn't be tested in that environment? |
I'm mentioning fetch-mock because I was migrating from it to mswjs. Previously we had some test files which were run under node environment with fetch-mock. I assume node environment was used for performance reasons. So after migrating to mswjs I tried to keep node environment but now I understand that it's probably a wrong way to use node environments for tests which use browser APIs like fetch |
es complejo hacer funcionar msw y whatwg-fetch (porque node no tiene fetch) debido a que son paquetes preparados para funcionar con commonjs y no con esm, supongo que por la naturaleza de node, que no ha implementado esm hasta hace pocas versiones. de todas formas, lo que me ha dado la clave para hacerlo funcionar han sido dos factores: - el primero es haber encontrado [este hilo](mswjs/msw#686 (comment)) - y el segundo ha sido haber sido notificado por el propio vitest o jest (no estoy seguro), indicándome como hacerlo, mírese el archivo vitest.config.ts (11-15)
@kettanaito I was stuck for a whole day facing the same issue with |
Hi, @abdofola. No, MSW never stated to mock any request-issuing clients. In fact, not doing so is one of the things that sets MSW apart from the alternatives. But even with those, you must ensure your test environment can run your code, which often means polyfilling |
kettanaito https://dev.to/cloudx/nodejs-18-fetch-api-test-runner-module-and-more-2ckg |
@tmerlet, the global fetch will make things much easier but MSW doesn't support it yet. The functionality is implemented and we're planning on releasing it sometime soon. |
@kettanaito thank you so much for your example with But before I tried to use
|
@VadimZP thank you! Adding only |
By the way for people still coming here, starting from My problem was that it was working locally on my machine (running |
@Nargonath, unfortunately, when using Jest, people still have to explicitly map |
Alright thanks @kettanaito for the clarification. I'm using But they don't a polyfill anymore, do they? They just need to add the config to |
This solved it for me. It was a combination of needing to use Thanks! The server for Jest tests (React pinging a Rails API) works wonderfully! |
Environment
| msw | 0.28.1 |
Request handlers
I'm currently using msw to mock fetch in my jest test. The component uses the default javascript fetch and works fine with that.
When I run the test without adding node-fetch to my component, the test fails : ReferenceError: fetch is not defined
When I run the test with "const fetch = require("node-fetch");" in my component file, the test works fine (but the component breaks when it's rendered in a browser so it can't be a solution for me)
Also, I noticed that if I don't use node-fetch but I install jest-fetch-mock and I disable it (to still use MSW), then things work
require('jest-fetch-mock').enableMocks() fetchMock.dontMock()
Without node-fetch or jest-fetch-mock it says fetch is not defined (but the component works fine with the javascript fetch)
I'm so confused, installing but not using jest-fetch-mock make the error go away and why do I need all this, why can't I mock the window.fetch with msw ?
The text was updated successfully, but these errors were encountered: