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-axiosNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Sets up a manual Jest mock for axios and shows a basic test that resolves a request synchronously with mockAxios.mockResponse.
Use v4.5.0 or upgrade to v4.6.1+.
Migrate to using `mockAxios.getReqByMatchUrl` or `mockAxios.lastPromiseGet` with caution; prefer using request handler callback.
Create `__mocks__/axios.js` as shown in the README and call `jest.mock('axios')` in your test file before importing the module under test.Ensure you call `mockAxios.mockResponse` or `mockError` immediately after triggering the axios call, within the same synchronous test block.
Use a more comprehensive mock or manually mock interceptors if needed. Consider libraries like `axios-mock-adapter` for more advanced scenarios.
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.Verify you created `__mocks__/axios.js` with `export default mockAxios` and that you import `mockAxios` from `jest-mock-axios` inside that file.
Add `transformIgnorePatterns: ['node_modules/(?!(jest-mock-axios)/)']` to Jest config, or use a version that ships both CJS and ESM (v4.6.1+).
Make sure you called `jest.mock('axios')` before importing your module under test, and that the mock file exports the mock axios instance.