jest-fetch-mock icon indicating copy to clipboard operation
jest-fetch-mock copied to clipboard

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

Open sekmo opened this issue 3 years ago • 1 comments

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 avatar Mar 01 '22 15:03 sekmo

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 :)

sekmo avatar Mar 01 '22 18:03 sekmo