axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

Possible to throw an error if a request was made but not mocked?

Open createthis opened this issue 6 years ago • 1 comments

I've used other mocking libraries in the past with other languages. One of the features I really miss is the ability to have my mocking library make my test fail if a request was made, but not mocked.

Ideally, axios-mock-adapter would report the request that was made, but not mocked, so that I could either:

  1. Add an appropriate mock
  2. Fix my code so that this unexpected axios call no longer happens.

Is this currently possible with axios-mock-adapter?

createthis avatar Oct 30 '18 16:10 createthis

I think this would achieve what you're after:

// Register this as the last handler, which will catch any unmatched request
mock.onAny().reply(function(config) {
  throw new Error('Could not find mock for ' + config.url);
});

instance.get('/foo');

It would of course be more convenient if this could be defined when the mock is created, but there's no support for that yet.

ctimmerm avatar Oct 30 '18 17:10 ctimmerm