Fetch-Mock or Jest-Fetch-Mock does not work with the latest fetch-retry
sangeetha5491 opened this issue · 7 comments
I tried the latest fetch-retry version. After the first invocation of fetch, when it goes into retries, it does not make the call again, or i assume its making the call to the actual endpoint and the request times out.
Are you using fetch in a global or non-global setting?
http://www.wheresrhys.co.uk/fetch-mock/#usageglobal-non-global
I suspect with the 3.x version of fetch-retry
you need to configure fetch-mock
to run in a non-global setting and use the sandbox()
feature, and then wrap the sand-boxed fetchMock into the fetch-retry
constructor function.
const fetchMock = require('fetch-mock');
const mockedFetch = fetchMock.sandbox().mock('/home', 200);
const fetch = require('fetch-retry')(mockedFetch);
I'm using the nonglobal approach. Let me try it out again with what you suggested.
I tried it, i think im missing some configuration detail and its not working. Still trying to figure it out
Did not work with fetch-mock. Used jest-fetch-mock instead. I had to add jest.useRealTimers()
I'm using fetch-retry
inside a React component and I'm trying to test said component with Jest. I'm using jest-fetch-mock
, but just can't make it work with fetch-retry
.
I'm trying to test this scenario:
fetch-retry
has retryOn: [202]
. So I want to mock multiple fetches, something like this:
fetch.mockResponses(
[JSON.stringify({}), { status: 202 }],
[JSON.stringify({}), { status: 202 }],
[JSON.stringify({}), { status: 200 }]
);
So should have 2 retries, and the 3rd should be ok.
I just can't get this working. It seems that it never goes beyond the first fetch call. If I change the first response to status: 200
then it works, because there is no retry.
Any tips?
Is it possible to use something like nock when testing React components? If so, it might work better than using a higher level mocking library, because nock
is overriding the underlying http.request
function of node.
Alternatively, maybe doing a couple of setImmediate
invocations might do the trick, in case the subsequent iterations in the event loop is not executed within your test for some reason.
I'm using
fetch-retry
inside a React component and I'm trying to test said component with Jest. I'm usingjest-fetch-mock
, but just can't make it work withfetch-retry
.I'm trying to test this scenario:
fetch-retry
hasretryOn: [202]
. So I want to mock multiple fetches, something like this:fetch.mockResponses( [JSON.stringify({}), { status: 202 }], [JSON.stringify({}), { status: 202 }], [JSON.stringify({}), { status: 200 }] );So should have 2 retries, and the 3rd should be ok.
I just can't get this working. It seems that it never goes beyond the first fetch call. If I change the first response to
status: 200
then it works, because there is no retry.Any tips?
Hi @rodrigovallades I know this is an old issue but were you able to make this work with a react component by any chance? i have the same exact usecase and while the jest testing is working if I test my wrapper function that consumes fetch-retry, my react component that is consuming said wrapper function only is recognizing the first API call, and none of the retries.