ctimmerm / axios-mock-adapter

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

onNoMatch doesn't throw error when there is a catch block #292

Closed ivanfuzuli closed 3 years ago

ivanfuzuli commented 3 years ago

I use axiosMockAdapter with { onNoMatch: "throwException" } config. If there is no catch block it throws error as expected. But if there is a catch in promise chain it doesn't throw anything.

This throws error.

import MockAdapter from "axios-mock-adapter";
import axios from "axios";

const axiosMock = new MockAdapter(axios, { onNoMatch: "throwException" });

axiosMock.onGet("blabla").replyOnce(201);
axios.get("not-found");

This doesn't throw.

import MockAdapter from "axios-mock-adapter";
import axios from "axios";

const axiosMock = new MockAdapter(axios, { onNoMatch: "throwException" });

axiosMock.onGet("blabla").replyOnce(201);
axios.get("not-found").catch(() => {});
ctimmerm commented 3 years ago

This is expected as that's how JavaScript works. The .catch in the second example will catch the error and thus the error won't further go down the chain.

If you don't want that to happen you can rethrow the error in the catch:

axios.get("not-found").catch((error) => {
  // Handle errors you can recover from
  // ...

  // Rethrow any errors that you haven't handled
  throw error;
});