jefflau / jest-fetch-mock

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

support buffers when mocking responses #223

Open alexkolson opened 2 years ago

alexkolson commented 2 years ago

👋 @yinzara

This would add support for the issue referenced in #218. This would be really useful for me and perhaps others as well! Let me know if this looks okay and/or if I should implement in a different way. Thanks!

alexkolson commented 2 years ago

@andreasg123 Took another shot at it. What do you thnk?

alexkolson commented 2 years ago

@andreasg123 Unfortunately if we swap out the check to go in normalizeResponse then it would have to be in two places no? Swapping the check to that location results in the test failing that checks that you can do:

fetchMock.mockResponse(new Response('foo'))

I am perhaps missing something and welcome your continued feedback. Sorry if I wasn't able to grasp what you were talking about on the first try!

andreasg123 commented 2 years ago

@alexkolson, I'm concerned about these cases:

fetchMock.mockResponse(request => new Response('foo'));
fetchMock.mockResponse(request => Promise.resolve(new Response('foo')));

I don't think that those would work without the change that I suggested. For those examples, isFn(bodyOrFunction) in normalizeResponse would be true whereas your example wouldn't take that path.

andreasg123 commented 2 years ago

As typeof resp === 'string' would be false, a new Response object would be constructed from resp.body, losing all the other information in the original Response object.