-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fetchBaseQuery with jest + msw results in ReferenceError: Request is not defined
#2084
Comments
I believe you need to import the polyfill before the api gets created. Is your import in a setupTests file (jest: setupFilesAfterEnv) or in a local test file? |
I tried both. None worked. |
Are you sure you have setupFilesAfterEnv set (if you do not use something like CRA)? I added whatwg-fetch exactly the same way (in our setupTests.js) just yesterday and it works. Some more information about your setup might be interesting then. |
Yes, the problem was indeed a missing setupFilesAfterEnv in our vite.js setup. Changing in our setupFilesAfterEnv: ["@testing-library/jest-dom"], to setupFilesAfterEnv: ["@testing-library/jest-dom", "./jest.setup.ts"], with import "whatwg-fetch"; (and previously installing/adding "whatwg-fetch" with npm) makes it work. So I am closing. Though it would not hurt to add a paragraph somewhre in the official docs adding this (sorry, for having no time to do it myself) Thank you! Great toolkit! |
We are migrating towards using RTK Query for API calls, and using `msw` to mock APIs in our tests. RTK Query's `fetchBaseQuery` requires the use of `fetch` and `Request`. `fetch()` was added to Node in v18, and the ability to use it in Jest tests was added to Jest in v28. However, it still cannot be used with a `jsdom` test environment. Therefore, it is necessary to add a polyfill. There are several libraries available for this but many others in this situation have had success using `whatwg-fetch` and so it was selected somewhat arbitrarily. It is also important that `whatwg-fetch` is imported before `fetchBaseQuery` (otherwise, a nuisance console warning will be issued). To ensure this happens, it is imported in a Jest setup file. References: reduxjs/redux-toolkit#2084 reduxjs/redux-toolkit#1271 jestjs/jest#13834
Adding a comment to hopefully save anyone who finds this issue while troubleshooting these warnings/errors some time. (!) at the time of writing this, This issue was raised in March 2022. Jest v28 was released in April 2022, after this post. Jest 28 adds support for |
<!-- Thanks for sending a pull request! --> #### What this PR does / why we need it: To prevent fetch warnings on jest: reduxjs/redux-toolkit#2084 #### Which issue(s) does this PR fixes?: <!-- (Optional) Automatically closes linked issue when PR is merged. Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. --> Fixes # #### Additional comments?:
I have seen #1271 (comment) and read through the whole thread. Even with all these workarounds I get:
followed by:
My code is as follows:
and
Naturally, I would be interested in getting the test to work. More generally, could you maybe provide some documentation on how to setup jest test using RTK Query?
The text was updated successfully, but these errors were encountered: