Registry / testing / axios-hooks-mock

axios-hooks-mock

JSON →
library1.1.0jsnpmunverified

A library for simplifying mocking of axios-hooks in tests, especially when multiple hooks are used together. Current stable version: 1.1.0. Release cadence is low (last release likely 2020). Differentiator: provides a fluent API to mock multiple axios-hook calls with different configs (method, url, params), supports convenience methods like .get() and .addImplementation(), and allows default fallback implementations. Built in TypeScript with full test coverage. Requires peer dependencies: react >=16.9.0, axios, and axios-hooks >=2.0.0.

npm install axios-hooks-mock
INSTALL
IMPORT
SIG · AXIOS-HOOKS-MOCK
A
axios-hooks-mock
testingjavascriptv1.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.

AxiosHooksMock
import AxiosHooksMock from 'axios-hooks-mock';
const AxiosHooksMock = require('axios-hooks-mock');
Package exports a default class. CommonJS require works but TypeScript may treat it differently.
AxiosHooksMockItem
import AxiosHooksMock, { AxiosHooksMockItem } from 'axios-hooks-mock';
import { AxiosHooksMockItem } from 'axios-hooks-mock';
AxiosHooksMockItem is a named type export; often forgotten.
AxiosHooksTuple
import type { AxiosHooksTuple } from 'axios-hooks-mock';
import { AxiosHooksTuple } from 'axios-hooks-mock';
AxiosHooksTuple is a type, so use import type to avoid runtime issues.

Shows how to mock a single GET request using the convenience method, with jest and ts-jest.

jest.mock('axios-hooks'); import AxiosHooksMock from 'axios-hooks-mock'; import { mocked } from 'ts-jest/utils'; import useAxios from 'axios-hooks'; const refetch = jest.fn(); mocked(useAxios).mockImplementation( new AxiosHooksMock() .get('https://testapi.com', [ { data: { testData: 'theData' }, loading: false }, refetch ]) .implement() );
Debug
Known issues
gotchaMust call jest.mock('axios-hooks') before mocking. The library does not do this automatically.
fix
Add jest.mock('axios-hooks') at the top of your test file.
affects: >=1.0.0
breakingRemoved useAxios mock integration in v1.1.0; now requires manual mock of useAxios via jest.spyOn or similar.
fix
Use mocked(useAxios).mockImplementation(...) instead of relying on the library to mock useAxios.
affects: >=1.1.0
deprecatedConvenience methods .get(), .post(), etc. are deprecated in v1.1.0 in favor of .addImplementation() with explicit method.
fix
Use .addImplementation({ url, method: 'GET' }, ...) instead.
affects: >=1.1.0
gotchaTypeScript users need to import AxiosHooksTuple as a type, not a value. Fails at runtime if used as value.
fix
Use import type { AxiosHooksTuple } from 'axios-hooks-mock';
affects: >=1.0.0
gotchaIf no implementation matches the request config, default implementation (if set) is used; otherwise returns undefined. This can cause confusing test failures.
fix
Set a default implementation with .default() or provide complete set of implementations.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: axios_hooks_mock_1.default is not a constructor
CommonJS require returns the module object, not the default export directly.
fix
Use import AxiosHooksMock from 'axios-hooks-mock'; or const { default: AxiosHooksMock } = require('axios-hooks-mock');
Cannot find module 'axios-hooks' from 'test.js'
Missing jest.mock('axios-hooks') or peer dependency not installed.
fix
Install axios-hooks as a devDependency and add jest.mock('axios-hooks') to test.
TypeError: (0 , ts_jest_utils_1.mocked) is not a function
ts-jest/utils.mocked was renamed in ts-jest v27 to just 'mocked' from 'ts-jest'. Using older import path.
fix
Use import { mocked } from 'ts-jest'; (ts-jest v27+) or keep using 'ts-jest/utils' if on older version.
Property 'data' does not exist on type 'undefined'
No default implementation set and no matching implementation found, returning undefined.
fix
Provide a default implementation with .default() or ensure all configs are matched.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
reactrequiredpeer dependency for axios-hooks; required at runtime
axiosrequiredpeer dependency for axios-hooks; required at runtime
axios-hooksrequiredpeer dependency; the library mocks this package
Agent activity
17 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
axios-hooks-mock — npm install axios-hooks-mock · libregistry