You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You should see the error "ReferenceError: fetch is not defined"
Expected behavior
I expect to see the following results:
PASS ./script.test.js
✓ testing fetch (670 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Actual behavior
The test is failing with the error message "ReferenceError: fetch is not defined".
However, when you remove the line testEnvironment: 'jsdom' from jest.config.jseverything is working as expected.
Additional context
Due to this behavior we are forced to polyfill the Fetch API (fetch, Response, Request, ...) at my workplace. This is becoming progressively more tedious. node-fetch/node-fetch#1263
Environment
System:
OS: Linux 6.1 Arch Linux
CPU: (8) x64 AMD Ryzen 5 PRO 3500U w/ Radeon Vega Mobile Gfx
Binaries:
Node: 19.4.0 - /usr/bin/node
Yarn: 1.22.19 - /usr/bin/yarn
npm: 8.19.2 - /usr/bin/npm
npmPackages:
jest: ^29.4.1 => 29.4.1
The text was updated successfully, but these errors were encountered:
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#2084reduxjs/redux-toolkit#1271jestjs/jest#13834
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
Version
29.4.1
Steps to reproduce
Expected behavior
I expect to see the following results:
Actual behavior
The test is failing with the error message "ReferenceError: fetch is not defined".
However, when you remove the line
testEnvironment: 'jsdom'
fromjest.config.js
everything is working as expected.Additional context
Due to this behavior we are forced to polyfill the Fetch API (
fetch
,Response
,Request
, ...) at my workplace. This is becoming progressively more tedious. node-fetch/node-fetch#1263Environment
System: OS: Linux 6.1 Arch Linux CPU: (8) x64 AMD Ryzen 5 PRO 3500U w/ Radeon Vega Mobile Gfx Binaries: Node: 19.4.0 - /usr/bin/node Yarn: 1.22.19 - /usr/bin/yarn npm: 8.19.2 - /usr/bin/npm npmPackages: jest: ^29.4.1 => 29.4.1
The text was updated successfully, but these errors were encountered: