ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

ivanfuzuli opened this issue · comments

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(() => {});

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;
});