Registry / testing / jest-axios

jest-axios

JSON →
library0.2.1jsnpmunverified

jest-axios is a Jest testing utility (v0.2.1, last released in 2020) that simplifies mocking REST API requests made with Axios. It provides a declarative way to define reusable mock endpoints, internal data storage, and helpers for collections and models. Unlike manual Jest mock functions or libraries like msw (which intercepts at the network level), jest-axios overrides Axios specifically, making it lighter for Axios-heavy projects. However, it relies on Jest's automock feature for Axios, which can cause issues with imports. The package appears to be in maintenance mode with infrequent updates.

npm install jest-axios
INSTALL
IMPORT
SIG · JEST-AXIOS
J
jest-axios
testingjavascriptv0.2.1
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.

Server
import { Server } from 'jest-axios'
const Server = require('jest-axios').Server
ESM import is standard; CommonJS require also works but ESM is recommended for Jest's automock.
Server
const { Server } = require('jest-axios')
const Server = require('jest-axios')
CommonJS destructuring required; default export does not exist.
default
import jestAxios from 'jest-axios'
import { default } from 'jest-axios'
Package does not have a default export. This import will fail.

Shows how to define a mock server with data and endpoints, then use it in a Jest test with Axios.

// tests/index.test.js import axios from 'axios'; import { Server } from 'jest-axios'; class App extends Server { data() { return { todos: [{ name: 'foo', done: false }, { name: 'bar', done: true }] }; } api() { return { '/todos': this.collection('todos'), '/todos/:id': this.model('todos') }; } } const server = new App('todo-app'); jest.mock('axios'); server.init(axios); test('gets todos', async () => { const response = await axios.get('/todos'); expect(response.status).toBe(200); expect(response.data).toEqual([{ id: 1, name: 'foo', done: false }, { id: 2, name: 'bar', done: true }]); });
Debug
Known issues
gotchajest.mock('axios') must be called before importing modules that use axios, otherwise axios may not be mocked properly.
fix
Call jest.mock('axios') at the top of your test file, before any imports that use axios.
affects: >=0.0.0
gotchaServer.init(axios) only works if jest.mock('axios') has been called first; otherwise, axios is not mocked.
fix
Ensure jest.mock('axios') is called before server.init(axios).
affects: >=0.0.0
deprecatedThe package has not been updated since 2020; newer Jest and Axios versions may cause compatibility issues.
fix
Consider alternative like msw or jest-mock-axios for newer projects.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot read property 'get' of undefined
jest.mock('axios') was not called before server.init(axios), or the mock was not applied.
fix
Add jest.mock('axios') at the top of your test file and ensure it is called before server.init(axios).
Cannot find module 'jest-axios' from 'tests/index.test.js'
jest-axios is not installed.
fix
Run 'npm install jest-axios --save-dev' or 'yarn add jest-axios --dev'.
TypeError: Class extends value undefined is not a constructor or null
Server import is incorrect (e.g., default import).
fix
Use named import: import { Server } from 'jest-axios'
Upgrade
Version history
0.2.1latest on npm
Audit
Dependencies
jestoptionalPeer dependency for Jest mocking utilities
axiosrequiredRuntime dependency for Axios mocking
Agent activity
9 hits · last 30 days
node
8
Resources
jest-axios — npm install jest-axios · libregistry