ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.47k stars 245 forks source link

Added ability to passThrough() if its set at beginning of all handlers #217

Closed meetromb closed 4 years ago

meetromb commented 5 years ago

Example of a problem:

const mock = new MockAdapter(instance)
mock.onAny().passThrough()

mock
    .onGet('/foo')
    .reply(200, 'bar')

mock
    .onGet('/bar')
    .reply(200, 'foo')

In this case every request will be forwarded to origin instance.

According to the documentation you need to do this:

const mock = new MockAdapter(instance)

mock
    .onGet('/foo')
    .reply(200, 'bar')

mock
    .onGet('/bar')
    .reply(200, 'foo')

mock.onAny().passThrough()

But this is inconvenient, because the code can be in different files and run out of order. This PR fixes this.

ctimmerm commented 4 years ago

This would be a breaking change and make the API inconsistent, I think it would be better if there was a separate way to define what should be done when no matching handler is found. Maybe this could be an option that is provided when the mock adapter is created, what do you think?

ostapenko-me commented 4 years ago

This would be a breaking change and make the API inconsistent, I think it would be better if there was a separate way to define what should be done when no matching handler is found. Maybe this could be an option that is provided when the mock adapter is created, what do you think?

I corrected this remark in the request #237. Can you check it out?

ctimmerm commented 4 years ago

Let's continue this in #237.