knee-cola / jest-mock-axios

Axios mock for Jest
252 stars 42 forks source link

Multiple server responses #1

Closed rnemec closed 5 years ago

rnemec commented 6 years ago

This is probably a feature request: If the module I'm testing makes multiple axios calls, could the dev have more control for mocking responses based on request data? I guess if the calls order is guaranteed, your API still works (I like the queue approach you did) but if they are done e.g. using Promise.all or some other less predictable ways, I wonder what you could offer. 😄

knee-cola commented 6 years ago

Requests can be resolved out of order by providing the second parameter to the mockResponse or mockError methods.

In the latest version I've added the lastReqGet which can be used to fetch the internal queue data item, which contains all the info regarding the request made.

If you store this object in you spec script you can use it to resolve previous requests in any order. The data contained in the object can be used to determine the contents of each request.

Does this help?

mshez commented 5 years ago

I bump into same problem. i have 4 API calls in ComponentDidMount and i want to control the response of each. lastReqGet doesn't help me.