redux-toolkit: RTKQ: fetchFn using cross-fetch fails
When using fetchFn
from createApi
, isomorphic-fetch works but cross-fetch fails.
Both seem to run fine in react without RTKQ. But with cross-fetch+RTKQ, a request that should be:
/some-endpoint
is run in the browser as /[object%20Request]
.
Possible I’m an idiot and flubbing the cross-fetch import and that’s the problem, but I’ve tried a few different import/require default/destructured variations and none seem to solve the issue.
...
const { fetch } = require("cross-fetch"); // doesn't work
// const fetch = require("isomorphic-fetch"); // works
// Define a service using a base URL and expected endpoints
export const pokemonApi = createApi({
reducerPath: "pokemonApi",
baseQuery: fetchBaseQuery({
baseUrl: "https://pokeapi.co/api/v2/",
fetchFn: fetch,
}),
...
Minimal Codesandbox example
https://codesandbox.io/s/vibrant-resonance-cvf7m?file=/src/services/pokemon.ts
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (5 by maintainers)
Commits related to this issue
- Jest: Polyfill fetch and Request 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 `Requ... — committed to lucasgarfield/image-builder-frontend by lucasgarfield a year ago
- Start to use msw to mock server requests. So far have had to stop jsdom declaring that tests are running in a browser: https://github.com/mswjs/msw/issues/1786 Fiddle with whatwg-fetch polyfill: h... — committed to jg210/spring-experiments by jg210 5 months ago
Moved
import 'whatwg-fetch';
to the very first line of Jest’ssetupTests.ts
file and the warning was gone 🥳 Thanks!import 'whatwg-fetch';
perfectly solve the problem in node test env using mswThe following works fine for me:
jest.setup.js
server.js
Wow. 3 hours of my life and that was the solution. Thank you. Testing the frontend is the hardest part of software development. 1 day to build the feature. 1 week if you try to write tests.
Had the same issue and adding
import 'isomorphic-fetch';
into setup file helped for me
It matters when you do that. If
fetchBaseQuery
is executed before'whatwg-fetch'
is included, you will get that message. It will work anyways, since it tries to access the globalfetch
later, but technically it is not available in the beginning.So import order plays a role there
@donatoaguirre24 That is a super slick test setup. I was passing isomorphic-fetch to fetchFn and making actual API calls, but this is 100X better. I avoid needing cross-fetch in my bundle and my tests are fast thanks to msw.
Thanks for all the help @phryneas @msutkowski @donatoaguirre24, and to the team for RTK. RTK is so good I feel guilty using it, esp now w/ RTKQ and its open API codegen.
Ah… I think I remember something from the old repo: https://github.com/rtk-incubator/rtk-query/issues/88#issuecomment-743797233
It seems that
cross-fetch
does not set theRequest
onglobal
.Doing it by hand fixes that:
That’s weird - they both worked when I was testing all of these things - we even used cross-fetch in a test. I can investigate if necessary.
@RileyMShea Regarding testing with jest, feel free to look at the test setup for the repo. Jest runs in a node environment, so you’re going to have to polyfill… we use node-fetch:
This was the only way how I got my tests running. Thanks for that 👍 (TestingLibrary+MSW+RTKQ+Jest).
But I am still seeing the warning that fetch is not available:
Which is really puzzling, is this really the way to get fetchBaseQuery to “work” in a Jest environment?
@RileyMShea Pro tip: Just add
abort-controller
andcross-fetch
as dev dependencies so they are not bundled in the production codefetchBaseQuery
is doingcross-fetch
is usingnode-fetch
and that just does not supportRequest
argument signaturefetch(request)
, they just supportfetch(url, [init])
.https://github.com/node-fetch/node-fetch/blob/ffef5e3c2322e8493dd75120b1123b01b106ab23/src/index.js#L34-L37