jefflau / jest-fetch-mock

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

mockResponse should take a function as argument, whose result can be used as response #77

Closed tiger9502 closed 5 years ago

tiger9502 commented 6 years ago

this is to enable mocking a delayed response.

maybe even consider adding a second argument in mockResponse as response time in ms.

jefflau commented 6 years ago

Can you write up some pseudo code to explain what you mean? Why do you need the delayed response?

tiger9502 commented 6 years ago

sure... let's say you do fetch.mockResponse(JSON.stringify('my server is really slow'), 500) to mock a slow server that takes 500 ms to respond

jefflau commented 6 years ago

Apart from an edge case of slow servers - you'd probably want to mock most responses so they are fast so I'm uneasy changing the simpler API. Maybe it could be a new method as opposed to changing the current API. Something like fetch.mockDelayedResponse?

What would the Pseudo code look like if you used a function as an argument instead?

tiger9502 commented 6 years ago

it can be used to mock a simple webserver.

something like this: let username = 'user'; let email = 'test@test.com'; fetch.mockResponse(() => JSON.stringify({ message: Welcome, ${username}! Your email is ${email} })); and you can reassign username and email later and not worry about the mock.

On Sun, Jun 17, 2018 at 11:06 PM Jeff Lau notifications@github.com wrote:

Apart from an edge case of slow servers - you'd probably want to mock most responses so they are fast so I'm uneasy changing the simpler API. Maybe it could be a new method as opposed to changing the current API. Something like fetch.mockDelayedResponse?

What would the Pseudo code look like if you used a function as an argument instead?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jefflau/jest-fetch-mock/issues/77#issuecomment-397950774, or mute the thread https://github.com/notifications/unsubscribe-auth/ACgW-O6PJhnkMQXxCa0kSaWLNfGvGVvRks5t90NcgaJpZM4UrF26 .

jefflau commented 6 years ago

have you seen this PR by @lsimone?

https://github.com/jefflau/jest-fetch-mock/pull/75

Would this kind of implementation be similar to what you were suggesting?

lsimone commented 6 years ago

I guess this use case would be covered by my PR:

const delayResponse = (res, delay) => new Promise((resolve, reject) => setTimeout(() => resolve(res), delay))
fetch.mockResponse(delayResponse(JSON.stringify('my server is really slow'), 500))
tiger9502 commented 6 years ago

hi Jeff, yeah that'd be just fine. Thanks Jeff and Isimone!

Julien-Decoen commented 6 years ago

Hi @tiger9502, @jefflau ! Is this issue has been resolved ?

Thanks

franciscop commented 5 years ago

Is this or #75 going to move forward, @jefflau and @lsimone? This library is perfect, but this is blocking me as well. Is there a way I can help somehow?

lsimone commented 5 years ago

I want to provide the new PR but I didn't have time so far. I'll try to find it ASAP in the next 2 weeks!

tomevans18 commented 5 years ago

Is there any update on this? From my usage this is the last piece of the puzzle.

jefflau commented 5 years ago

I'd love to get this in, but I'm also a little busy these days. The discussion on this implementation is over here: https://github.com/jefflau/jest-fetch-mock/pull/75 and I think the general use case of either taking an object or a promise is a decent generalised API which would enable async support.

@lsimone is there any chance you will get some time to implement what we discussed back in June?

lsimone commented 5 years ago

@jefflau I'll try to deliver it this Sunday (or before if I can)

tomevans18 commented 5 years ago

Legend!

lsimone commented 5 years ago

@tomevans18 I found time to push my last commits. It can be merged IMO.

jefflau commented 5 years ago

This is now merged and published. Please take a look and test it out for us! I will close this issue soon after

tomevans18 commented 5 years ago

@lsimone epic work! @jefflau thanks for the update 👍

I will start using this and feedback any issues.

jefflau commented 5 years ago

@tomevans18 Working smoothly so far?

jefflau commented 5 years ago

Closing this issue. Assuming everything is working fine :)