jefflau / jest-fetch-mock

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

Mock conditionals #95

Open ivan-kleshnin opened 5 years ago

ivan-kleshnin commented 5 years ago

Is there a way to mock conditionally for cases with parallel requests? For example:

fetch.mockResponseOnce(...) // mocks first result, assuming "/blog/"
fetch.mockResponseOnce(...) // mocks second result, assuming "/blog/post/"

/*
now what if you don't know whether "/blog/" OR "/blog/post/" will be called first...
*/

I imagine something like

fetch.on("/blog/").mockResponseOnce(...)
fetch.on("/blog/post/").mockResponseOnce(...)

but maybe there's an alternative approach already. Searching through docs, issues and Google didn't highlight this for me.

jefflau commented 5 years ago

Hey @ivan-kleshnin currently there isn't a way to do this, but this seems like an interesting topic to explore.

eatspaint commented 5 years ago

+1 to this. Currently needing to test a handful of requests to different endpoints that happen in no particular order, and would love to have something to handle that.

eatspaint commented 5 years ago

I've come up with an initial pass at this issue in #102. Let me know if you have any thoughts @jefflau!