jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
886 stars 117 forks source link

Mocking 2 responses with the same url, but with different status #229

Open sekmo opened 2 years ago

sekmo commented 2 years ago

Hello everyone! Is it possible to mock 2 responses with the same method and URL, but making the first one returning a 400 and the second one a 200?

sekmo commented 2 years ago

Sorry, I just saw right now that I can use a function to process the response, so I guess that something like this would be the answer:

let amountOfCalledFetchMocks = 0
fetchMock.get('begin:http://test.test/', () => {
  amountOfCalledFetchMocks++;
  if(amountOfCalledFetchMocks == 1) {
    return 400;
  } else {
    return 200;
  }
});

Please let me know if you have a better solution, thanks :)