Registry / testing / moxios

moxios

JSON →
library0.4.0jsnpmunverified

Mock axios requests for testing. v0.4.0 is the latest stable release (infrequent updates). Allows you to install a mock adapter into axios, intercept requests, and respond with custom data. Unlike alternatives like axios-mock-adapter, moxios provides a higher-level API with `moxios.wait()` and `moxios.requests.mostRecent()`, making it easier to write integration tests that simulate asynchronous request handling. Requires axios (peer dependency). Limited to browser-like environments (uses XMLHttpRequest).

npm install moxios
INSTALL
IMPORT
SIG · MOXIOS
M
moxios
testingjavascriptv0.4.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.

moxios
import moxios from 'moxios'
const moxios = require('moxios')
ESM default export; CommonJS require works in Node but not with ES modules.
moxios
const moxios = require('moxios')
import { moxios } from 'moxios'
Named import is incorrect; moxios is a default export.
moxios.install
moxios.install()
moxios.install(axios)
install() optionally accepts a custom axios instance; calling without args uses the default axios.

Shows basic setup using install/uninstall, waiting for a request, and responding with custom data.

import axios from 'axios'; import moxios from 'moxios'; describe('moxios example', () => { beforeEach(() => moxios.install()); afterEach(() => moxios.uninstall()); it('mocks a GET request', (done) => { axios.get('/users').then((res) => { expect(res.data).toEqual([{ id: 1 }]); done(); }); moxios.wait(() => { let request = moxios.requests.mostRecent(); request.respondWith({ status: 200, response: [{ id: 1 }] }); }); }); });
Debug
Known issues
gotchamoxios.install() replaces the global XMLHttpRequest, interfering with other libraries that rely on it (e.g., fetch polyfills).
fix
Use moxios.uninstall() after each test and ensure no other XHR mocks are active.
affects: >=0.1.0
deprecatedmoxios.stubRequest() is deprecated in favor of using moxios.wait() with requests.mostRecent() or requests.get().
fix
Use moxios.wait() and manually assert on requests.
affects: >=0.4.0
breakingIn v0.4.0, respondWith() returns a Promise that must be awaited or used .then() to ensure response handling completes.
fix
Chain .then() after respondWith() or use async/await.
affects: 0.4.0
gotchamoxios.wait() does not work with fake timers (e.g., sinon.useFakeTimers) because it relies on real setTimeout.
fix
Avoid fake timers or use moxios.requests.mostRecent() synchronously if possible.
affects: >=0.1.0
Errors
Common errors & fixes
Cannot read property 'mostRecent' of undefined
moxios.install() was not called before accessing moxios.requests.
fix
Call moxios.install() in beforeEach or before the relevant test.
TypeError: moxios is not a function
Imported moxios as named import instead of default import.
fix
import moxios from 'moxios' (not import { moxios } from 'moxios').
Timeout - Async callback was not invoked within the 5000ms timeout
moxios.wait() callback never called, likely because the request was not made by axios.
fix
Ensure axios is using the same adapter (moxios installs). Check that the request URL matches.
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies
axiosrequiredpeer dependency: moxios mocks requests made by axios
Agent activity
4 hits · last 30 days
node
4
Resources
packagemoxios
moxios — npm install moxios · libregistry