The problem I'm seeing in your code is that expect().toThrowError() expects an synchronous exception, but your code is actually rejecting a promise. The bit that may be confusing is that you see throw new Error('foo'); in your handleResponse. That's throwing an exception, true. However, because this code is called by an async function this exception is converted to a promise rejection. So await get('sampleUrl') does not result in a synchronous exception being thrown but in an asynchronous promise rejection.

Looking at Jest's documentation, it seems to me that you should be doing:

return expect(expectedError()).rejects.toThrowError();

You need to call expectedError because it is a function that returns a promise, and you need to use .rejects to converts the rejection to a plain exception that can be tested with .toThrowError(). Make sure to return the assertion, or await it. Otherwise you'll have dangling promise.

Answer from Louis on Stack Overflow
🌐
npm
npmjs.com › package › axios-mock-adapter
axios-mock-adapter - npm
October 9, 2024 - const axios = require("axios"); const AxiosMockAdapter = require("axios-mock-adapter"); // This sets the mock adapter on the default instance const mock = new AxiosMockAdapter(axios); // Mock GET request to /users when param `searchText` is 'John' // arguments for reply are (status, data, headers) mock.onGet("/users", { params: { searchText: "John" } }).reply(200, { users: [{ id: 1, name: "John Smith" }], }); axios .get("/users", { params: { searchText: "John" } }) .then(function (response) { console.log(response.data); });
      » npm install axios-mock-adapter
    
Published   Oct 09, 2024
Version   2.1.0
Author   Colin Timmermans
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › releases
Releases · ctimmerm/axios-mock-adapter
October 9, 2024 - Axios adapter that allows to easily mock requests. Contribute to ctimmerm/axios-mock-adapter development by creating an account on GitHub.
Author   ctimmerm
Discussions

Expecting an error with axios-mock-adapter
I am trying to test an axios get request with axios-mock-adapter, such that an error is thrown given a status that does not equal 200. However, when I execute the test (see api.test.js), I get the More on stackoverflow.com
🌐 stackoverflow.com
unit testing - How do you verify that a request was made with axios-mock-adapter? - Stack Overflow
I am using https://github.com/ctimmerm/axios-mock-adapter I would like to know how can I verify that an endpoint was actually called by the system under test. In this example: var axios = require('... More on stackoverflow.com
🌐 stackoverflow.com
Newest 'axios-mock-adapter' Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
React-Testing-Library - What do you guys use to mock HTTP requests?
Maybe I’m not an extreme unit tester as the rest of the others but I personally just do something like jest.spyOn(window, “fetch”).mockReturnValue({foo : bar}) More on reddit.com
🌐 r/reactjs
8
6
February 12, 2021
🌐
Medium
simonkkaranja.medium.com › react-testing-mocking-axios-with-axios-mock-adapter-e24752a55923
React Testing: Mocking Axios with axios-mock-adapter | by Simon Karanja | Medium
December 14, 2020 - If so, when writing tests you may need to mock API requests. You could of course use mock functions provided by your test library in this case Jest, but I have found a nifty package called axios-mock-adapter to be excellent and natural when testing codes with axios implementation.
🌐
UNPKG
app.unpkg.com › axios-mock-adapter@1.21.1 › files › README.md
axios-mock-adapter
# axios-mock-adapter Axios adapter ... https://unpkg.com/axios-mock-adapter/dist/axios-mock-adapter.min.js axios-mock-adapter works on Node as well as in a browser, it works with axios v0.17.0 and above....
🌐
GitHub
github.com › ctimmerm › axios-mock-adapter › issues
ctimmerm/axios-mock-adapter
Axios adapter that allows to easily mock requests. Contribute to ctimmerm/axios-mock-adapter development by creating an account on GitHub.
Author   ctimmerm
Find elsewhere
🌐
CodeSandbox
codesandbox.io › examples › package › axios-mock-adapter
axios-mock-adapter examples - CodeSandbox
Use this online axios-mock-adapter playground to view and fork axios-mock-adapter example apps and templates on CodeSandbox.
🌐
Apidog
apidog.com › blog › react-mock-api-axios
Mastering API Testing with React Mock API and Axios
July 24, 2024 - We’ll use axios-mock-adapter, a simple library to mock Axios requests.
🌐
Stack Overflow
stackoverflow.com › questions › tagged › axios-mock-adapter
Newest 'axios-mock-adapter' Questions - Stack Overflow
I am trying to use axios-mock-adapter to mock the response I get when calling the Spotify API to request an access token. Here is the function that does just that...
🌐
Snyk
snyk.io › advisor › axios-mock-adapter › axios-mock-adapter code examples
Top 5 axios-mock-adapter Code Examples | Snyk
modularcode / modular-material-admin-react-pro / src / _api / _mocks / index.ts View on Github · const init = (instance: AxiosInstance) => { const mockAdapter = new MockAdapter(instance, { delayResponse: 200 }) usersMocks.init(mockAdapter) organizationsMocks.init(mockAdapter) return mockAdapter }
🌐
TechNetExperts
technetexperts.com › home › easily mock requests with axios mock adapter
How to Use Axios Mock Adapter to Test Network Requests
November 13, 2025 - AXIOS Mock adapter allows you to call API in the test file and you can define the expected response according to your need or the same response which you are getting while calling actual API.
🌐
C.S. Rhymes
csrhymes.com › 2022 › 03 › 09 › mocking-axios-with-jest-and-typescript.html
Mocking axios in Jest tests with Typescript | C.S. Rhymes
March 9, 2022 - I started by following the example in the article and added jest.mock('axios'); after the imports.
🌐
CloudDefense.ai
clouddefense.ai › code › javascript › example › axios-mock-adapter
Top 10 Examples of axios-mock-adapter code in Javascript
beforeEach(() => { loadFixtures(fixture); // Re-declare this here so that test_setup.js#beforeEach() doesn't // overwrite it. mockAxios = new MockAdapter(axios); $.ajax = () => { throw new Error('$.ajax should not be called through!'); }; // These jQuery+DOM tests are super flaky so increase the timeout to avoid // random failures.
🌐
npm Trends
npmtrends.com › axios-mock-adapter
axios-mock-adapter | npm trends
Comparing trends for axios-mock-adapter 2.1.0 which has 1,921,783 weekly downloads and 3,551 GitHub stars.
🌐
Daniel Gustaw
gustawdaniel.com › notes › how-to-mock-axios
Daniel Gustaw - How to mock Axios in Jest
import axios from "axios"; import MockAdapter from "axios-mock-adapter"; import {describe, beforeAll, afterEach,it, expect} from "@jest/globals"; import {getMyHexIp} from "./getMyHexIp"; describe("External axios call", () => { let mock: MockAdapter; beforeAll(() => { mock = new MockAdapter(axios); mock.onGet(`https://ipinfo.io/ip`).reply(200, '69.69.69.69'); }); afterEach(() => { mock.reset(); }); it("getMyHexIp", async () => { // when const result = await getMyHexIp() // then expect(mock.history.get[0].url).toEqual(`https://ipinfo.io/ip`); expect(result).toEqual('0x45454545'); }); });
🌐
npms
npms.io › search
axios-mock-adapter
npms was built to empower the javascript community by providing a better and open sourced search for node modules.
🌐
Medium
medium.com › @zach.grusznski › how-to-use-axios-mock-adapter-to-test-network-requests-in-react-475e99cda5ea
How to use Axios Mock Adapter to test network requests in React | by Zach Grusznski | Medium
June 10, 2018 - We’ll use enzyme-adapter-react-16 in a moment to do our set up, and axios-mock-adapter is how we will mock our API call in order to test it.
🌐
Reddit
reddit.com › r/reactjs › react-testing-library - what do you guys use to mock http requests?
r/reactjs on Reddit: React-Testing-Library - What do you guys use to mock HTTP requests?
February 12, 2021 -

I tried:

axios-mock-adapter

Axios test fails when you use a proxy in your package.json.

    Error: connect ECONNREFUSED 127.0.0.1:80

Other then that, it works great. But I'm unable to use my CI because it outputs the error above too.

Nock

Works great, but I can't seem to mock requests with headers sent (such as authorization). I find it hard to find information on how to solve the issue.

I'm wondering if there are other libraries I should try out (or bonus points if there is a solution to the problems above :)!