Registry / testing / vitest-mock-axios

vitest-mock-axios

JSON →
library0.1.0jsnpmunverified

Lightweight, synchronous Axios mock for Vitest testing framework. Current stable version is 0.1.0. Provides a simple API (mockResponse, mockError, lastReqGet, etc.) to simulate Axios requests synchronously in unit tests, making test logic easier to read and write. Key differentiators include synchronous promise resolution (no need to await or use microtasks) and full Vitest spy integration. Requires manual setup of a Vitest module mock in __mocks__/axios.js. Comparatively, alternatives like msw or nock are more feature-rich but typically asynchronous and heavier.

npm install vitest-mock-axios
INSTALL
IMPORT
SIG · VITEST-MOCK-AXIOS
V
vitest-mock-axios
testingjavascriptv0.1.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 'vitest-mock-axios'
const mockAxios = require('vitest-mock-axios')
ESM-only. In Vitest tests, use default import after setting up module mock.
mockAxios
vi.mock('axios', () => import('vitest-mock-axios'))
vi.mock('axios', () => require('vitest-mock-axios'))
Do NOT use require() in vi.mock factory; must return a promise or use dynamic import.
MockAxios
import type { MockAxios } from 'vitest-mock-axios'
import { MockAxios } from 'vitest-mock-axios'
MockAxios is a TypeScript type for the mock instance, not a runtime value. Use type import.

Set up Vitest mock for Axios, use mockResponse to resolve a pending request synchronously.

// 1. Setup module mock in test file vi.mock('axios', () => import('vitest-mock-axios')); import axios from 'axios'; // 2. Test a function that uses axios async function fetchUser() { return axios.get('/user'); } it('returns user data', async () => { const userData = { id: 1, name: 'John' }; const promise = fetchUser(); axios.mockResponse({ data: userData }); const result = await promise; expect(result.data).toEqual(userData); }); // 3. Reset between tests afterEach(() => { axios.reset(); });
Debug
Known issues
gotchaRequires manual setup of __mocks__/axios.js file; not automatic.
fix
Create file in __mocks__ directory as described in installation docs.
affects: >=0.0.0
breakingSynchronous mock: requests are resolved immediately; code expecting async behavior may break if not adapted.
fix
Ensure test code does not rely on microtask ordering; use mockResponse directly after triggering the request.
affects: >=0.0.0
gotchaIncompatible with Jasmine/Mocha out of the box; only Vitest supported.
fix
Fork source code to replace vi.fn() with spies from other frameworks.
affects: >=0.0.0
gotchaDoes not support Axios adapter cancellation tokens beyond basic mock; not feature-complete.
fix
For advanced cancellation logic, consider using a more comprehensive mock library.
affects: >=0.0.0
Errors
Common errors & fixes
ReferenceError: axios is not defined
Missing vi.mock('axios') or incorrect setup
fix
Add `vi.mock('axios', () => import('vitest-mock-axios'));` before using axios in test.
TypeError: mockAxios.mockResponse is not a function
Default export not returned from vi.mock factory or incorrectly imported
fix
Ensure factory returns the default export: `vi.mock('axios', () => import('vitest-mock-axios'));`
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies
axiosrequiredThe mock mimics Axios API and expects Axios as a peer dependency for type compatibility.
vitestrequiredUses Vitest vi.fn() for spy creation; environment specific.
Agent activity
7 hits · last 30 days
node
6
Resources
vitest-mock-axios — npm install vitest-mock-axios · libregistry