ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.44k stars 244 forks source link

Create onNoMatch=throwException option to throw exception when could not find mock for requested url #263

Closed rafaelcaviquioli closed 4 years ago

rafaelcaviquioli commented 4 years ago

This submission allows the new option throwException in onNoMatch instance configuration.

Current problem: When a request has no configured mock, is not possible to trace and identify the source of request.

This is the only one feedback received from axios-mock-adapter:

 Request failed with status code 404
    at createAxiosError (node_modules/axios-mock-adapter/src/utils.js:144:19)
    at Object.settle (node_modules/axios-mock-adapter/src/utils.js:124:22)
    at handleRequest (node_modules/axios-mock-adapter/src/handle_request.js:127:15)

Proposed solution Using onNoMatch option with throwException to throw an exception with details about request url and method when a request is made without match any handler.

This is the new console output:

Could not find mock for: 
{
  "method": "get",
  "url": "http://localhost/unexistent-path"
}

I believe this new option is helpful for developers who are working with a lot of mocks. Sometimes we forget to define a required mock or do mistakes when setup it. When it happens, is not possible to trace and identify the source of request.

The same need was mentioned in issue https://github.com/ctimmerm/axios-mock-adapter/issues/173 by @createthis

ramospedro commented 4 years ago

Any chance we're merging this soon?

coderek commented 4 years ago

Please merge this.

ctimmerm commented 4 years ago

Thanks!

connorkatz-mb commented 4 years ago

Did the types not get updated when this was merged?

need onNoMatch?: 'passthrough' | 'throwException';

the interface on master has,

interface MockAdapterOptions {
  delayResponse?: number;
  onNoMatch?: 'passthrough';
}