{"id":48218,"library":"jest-mock-axios","title":"jest-mock-axios","description":"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.","status":"active","version":"4.9.0","language":"javascript","source_language":"en","source_url":"https://github.com/knee-cola/jest-mock-axios","tags":["javascript","axios","jest","mock","typescript"],"install":[{"cmd":"npm install jest-mock-axios","lang":"bash","label":"npm"},{"cmd":"yarn add jest-mock-axios","lang":"bash","label":"yarn"},{"cmd":"pnpm add jest-mock-axios","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Mocks the axios library; users must have axios installed","package":"axios","optional":false}],"imports":[{"note":"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.","wrong":"const mockAxios = require('jest-mock-axios');","symbol":"default","correct":"import mockAxios from 'jest-mock-axios';"},{"note":"The mock is a default export, not a named export. Named import will result in undefined.","wrong":"import { mockAxios } from 'jest-mock-axios';","symbol":"mockAxios","correct":"import mockAxios from 'jest-mock-axios';"},{"note":"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.","wrong":"","symbol":"Type imports (TypeScript)","correct":"import mockAxios from 'jest-mock-axios';"}],"quickstart":{"code":"// __mocks__/axios.js\nimport mockAxios from 'jest-mock-axios';\nexport default mockAxios;\n\n// myComponent.test.js\nimport mockAxios from 'jest-mock-axios';\nimport myComponent from './myComponent';\n\n// Mock axios before imports in test\njest.mock('axios');\n\nafterEach(() => {\n  mockAxios.reset();\n});\n\nit('should resolve with data', () => {\n  const onResponse = jest.fn();\n  const onError = jest.fn();\n  \n  myComponent.fetchData().then(onResponse).catch(onError);\n  \n  // Simulate a successful response\n  mockAxios.mockResponse({ data: { users: [] } });\n  \n  expect(onResponse).toHaveBeenCalledWith({ data: { users: [] } });\n  expect(onError).not.toHaveBeenCalled();\n});","lang":"javascript","description":"Sets up a manual Jest mock for axios and shows a basic test that resolves a request synchronously with mockAxios.mockResponse."},"warnings":[{"fix":"Use v4.5.0 or upgrade to v4.6.1+.","message":"In v4.6.0 the module was inadvertently a pure ES module, causing CommonJS projects to break. Importing or requiring fails with ERR_REQUIRE_ESM.","severity":"breaking","affected_versions":"4.6.0"},{"fix":"Migrate to using `mockAxios.getReqByMatchUrl` or `mockAxios.lastPromiseGet` with caution; prefer using request handler callback.","message":"The methods `lastReqGet` and `lastPromiseGet` are deprecated in favor of newer API (though still available). Their return structure may change.","severity":"deprecated","affected_versions":">=4.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"all"},{"fix":"Ensure you call `mockAxios.mockResponse` or `mockError` immediately after triggering the axios call, within the same synchronous test block.","message":"Using `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.","severity":"gotcha","affected_versions":"all"},{"fix":"Use a more comprehensive mock or manually mock interceptors if needed. Consider libraries like `axios-mock-adapter` for more advanced scenarios.","message":"The mock does not support mocking axios `CancelToken` or interceptors out of the box. Attempting to use these features will lead to unexpected behavior.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"search_vec":"'4.9.0':15 'api':20 'axio':4,7,23,83 'call':36 'caus':70 'easier':46 'enabl':27 'es':68 'execut':30 'fix':75 'http':35 'inadvert':65 'issu':71 'javascript':82 'jest':2,13,55,61,84 'jest-mock-axio':1 'later':74 'librari':40,78 'lightweight':5 'make':44 'manual':54 'mock':3,8,22,39,56,85 'modul':69 'note':62 'primarili':59 'provid':17 'pure':67 'read':48 'real':34 'request':24 'requir':52 'respons':26 'setup':57 'ship':79 'simpl':19 'support':60 'synchron':6,28,43 'test':11,29,45 'type':81 'typescript':80,86 'unit':10 'unlik':37 'v4.6.0':63 'v4.6.1':72 'version':14 'wait':32 'without':31 'work':42 'write':50","created_at":"2026-06-07T16:55:29.996337+00:00","updated_at":"2026-06-07T16:55:29.996337+00:00","problems":[{"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.","cause":"The mock file is not placed correctly or jest.mock('axios') is not called, so Jest cannot resolve the manual mock.","error":"Cannot find module 'axios' from '__mocks__/axios.js'"},{"fix":"Verify you created `__mocks__/axios.js` with `export default mockAxios` and that you import `mockAxios` from `jest-mock-axios` inside that file.","cause":"The mock was not set up correctly; the import likely returned the real axios or undefined.","error":"TypeError: mockAxios.mockResponse is not a function"},{"fix":"Add `transformIgnorePatterns: ['node_modules/(?!(jest-mock-axios)/)']` to Jest config, or use a version that ships both CJS and ESM (v4.6.1+).","cause":"The `jest-mock-axios` package uses ESM, but your project is configured to use CommonJS (e.g., older Jest config or no transformIgnorePatterns adjustment).","error":"Jest encountered an unexpected token - SyntaxError: Cannot use import statement outside a module"},{"fix":"Make sure you called `jest.mock('axios')` before importing your module under test, and that the mock file exports the mock axios instance.","cause":"The axios method (e.g., `post`) returned undefined because the mock wasn't ready when the call was made.","error":"Cannot read property 'then' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/knee-cola/jest-mock-axios#readme","github":"https://github.com/knee-cola/jest-mock-axios","docs":null,"changelog":null,"pypi":null,"npm":"jest-mock-axios","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}