Registry / testing / jest-mock-axios

jest-mock-axios

JSON →
library4.9.0jsnpmunverified

Lightweight, synchronous Axios mock for unit testing with Jest, version 4.9.0. It provides a simple API to mock Axios requests and responses, enabling synchronous test execution without waiting for real HTTP calls. Unlike other mocking libraries, it works synchronously, making tests easier to read and write. It requires a manual Jest mock setup and primarily supports Jest. Note: v4.6.0 was inadvertently a pure ES module causing issues; v4.6.1 and later fix this. The library ships TypeScript types.

npm install jest-mock-axios
INSTALL
IMPORT
SIG · JEST-MOCK-AXIOS
J
jest-mock-axios
testingjavascriptv4.9.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import mockAxios from 'jest-mock-axios';
const mockAxios = require('jest-mock-axios');
The default export is the mock object. CommonJS require works but is not recommended in ES module projects. The library is ESM-first since v4.6.1.
mockAxios
import mockAxios from 'jest-mock-axios';
import { mockAxios } from 'jest-mock-axios';
The mock is a default export, not a named export. Named import will result in undefined.
Type imports (TypeScript)
import mockAxios from 'jest-mock-axios';
No separate type import needed; types are included in the default export. Use `import type { MockAxios } from 'jest-mock-axios'` if you need the type explicitly.

Sets up a manual Jest mock for axios and shows a basic test that resolves a request synchronously with mockAxios.mockResponse.

// __mocks__/axios.js import mockAxios from 'jest-mock-axios'; export default mockAxios; // myComponent.test.js import mockAxios from 'jest-mock-axios'; import myComponent from './myComponent'; // Mock axios before imports in test jest.mock('axios'); afterEach(() => { mockAxios.reset(); }); it('should resolve with data', () => { const onResponse = jest.fn(); const onError = jest.fn(); myComponent.fetchData().then(onResponse).catch(onError); // Simulate a successful response mockAxios.mockResponse({ data: { users: [] } }); expect(onResponse).toHaveBeenCalledWith({ data: { users: [] } }); expect(onError).not.toHaveBeenCalled(); });
Debug
Known issues
breakingIn v4.6.0 the module was inadvertently a pure ES module, causing CommonJS projects to break. Importing or requiring fails with ERR_REQUIRE_ESM.
fix
Use v4.5.0 or upgrade to v4.6.1+.
affects: 4.6.0
deprecatedThe methods `lastReqGet` and `lastPromiseGet` are deprecated in favor of newer API (though still available). Their return structure may change.
fix
Migrate to using `mockAxios.getReqByMatchUrl` or `mockAxios.lastPromiseGet` with caution; prefer using request handler callback.
affects: >=4.0.0
gotchaThe mock must be set up as a manual Jest mock in `__mocks__/axios.js`. Simply importing `jest-mock-axios` without this setup will not intercept axios calls.
fix
Create `__mocks__/axios.js` as shown in the README and call `jest.mock('axios')` in your test file before importing the module under test.
affects: all
gotchaUsing `mockAxios.mockResponse` or `mockError` after the promise settles will have no effect. The mock works synchronously, so you must call mock methods before the next tick.
fix
Ensure you call `mockAxios.mockResponse` or `mockError` immediately after triggering the axios call, within the same synchronous test block.
affects: all
gotchaThe mock does not support mocking axios `CancelToken` or interceptors out of the box. Attempting to use these features will lead to unexpected behavior.
fix
Use a more comprehensive mock or manually mock interceptors if needed. Consider libraries like `axios-mock-adapter` for more advanced scenarios.
affects: all
Errors
Common errors & fixes
Cannot find module 'axios' from '__mocks__/axios.js'
The mock file is not placed correctly or jest.mock('axios') is not called, so Jest cannot resolve the manual mock.
fix
Ensure you have a `__mocks__` folder at the project root (or inside `src` if using react-scripts) and that you call `jest.mock('axios')` in your test file.
TypeError: mockAxios.mockResponse is not a function
The mock was not set up correctly; the import likely returned the real axios or undefined.
fix
Verify you created `__mocks__/axios.js` with `export default mockAxios` and that you import `mockAxios` from `jest-mock-axios` inside that file.
Jest encountered an unexpected token - SyntaxError: Cannot use import statement outside a module
The `jest-mock-axios` package uses ESM, but your project is configured to use CommonJS (e.g., older Jest config or no transformIgnorePatterns adjustment).
fix
Add `transformIgnorePatterns: ['node_modules/(?!(jest-mock-axios)/)']` to Jest config, or use a version that ships both CJS and ESM (v4.6.1+).
Cannot read property 'then' of undefined
The axios method (e.g., `post`) returned undefined because the mock wasn't ready when the call was made.
fix
Make sure you called `jest.mock('axios')` before importing your module under test, and that the mock file exports the mock axios instance.
Upgrade
Version history
4.9.0latest on npm
Audit
Dependencies
axiosrequiredMocks the axios library; users must have axios installed
Agent activity
11 hits · last 30 days
node
10
Resources
jest-mock-axios — npm install jest-mock-axios · libregistry