Gathering detailed insights and metrics for axios-hooks-mock
Gathering detailed insights and metrics for axios-hooks-mock
Gathering detailed insights and metrics for axios-hooks-mock
Gathering detailed insights and metrics for axios-hooks-mock
A library that simplifies mocking for axios-hooks, especially when multiple hooks are used together.
npm install axios-hooks-mock
Typescript
Module System
Node Version
NPM Version
TypeScript (98.11%)
JavaScript (1.89%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
21 Commits
2 Watchers
5 Branches
1 Contributors
Updated on Jan 28, 2023
Latest Version
1.1.0
Package Id
axios-hooks-mock@1.1.0
Unpacked Size
113.03 kB
Size
12.72 kB
File Count
13
NPM Version
6.14.8
Node Version
12.19.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
3
29
A library that simplifies mocking for axios-hooks, especially when multiple hooks are used together.
This library has full test coverage to ensure each scenario works and is written in TypeScript.
npm i axios axios-hooks
npm i -D axios-hooks-mock
Full Examples are available in the tests folder
There are a few ways to use this library to achieve your complex mocking goals. Examples will use jest
, but this could also be used with other testing libraries. Any usage of mocked
is using ts-jest/utils
.
The library matches on a combination of method
, url
, and params
. You must create one implementation per each combination you want to satisfy.
Each method of adding implementations accepts either a string (the url) or an AxiosRequestConfig
object (this is the object you are used to, including url, method, params, data, etc.). The convenience methods only accept the url string (for hopefully obvious reasons).
You can use any combination of the below.
1jest.mock('axios-hooks');
Having the library do this would make it far less flexible.
1import AxiosHooksMock from 'axios-hooks-mock'; 2 3const refetch = jest.fn(); 4mocked(useAxios).mockImplementation( 5 new AxiosHooksMock() 6 .get('https://testapi.com', [ 7 { data: { testData: 'theData' }, loading: false }, 8 refetch 9 ]) 10 // Chain as many as you want 11 .implement() 12);
1import AxiosHooksMock from 'axios-hooks-mock'; 2 3const refetch = jest.fn(); 4mocked(useAxios).mockImplementation( 5 new AxiosHooksMock() 6 .addImplementation({ url: 'https://testapi.com', method: 'GET' }, [ 7 { data: { testData: 'theData' }, loading: false }, 8 refetch 9 ]) 10 // Chain as many as you want 11 .implement() 12);
This allows for a configuration array to be handed into the constructor like so:
1import AxiosHooksMock from 'axios-hooks-mock'; 2 3const refetch = jest.fn(); 4 5const implementations: AxiosHooksMockItem[] = [ 6 { 7 config: { url: 'https://testapi.com', method: 'GET' }, 8 implementation: [ 9 { data: { testData: 'theData' }, loading: false }, 10 refetch 11 ] 12 } 13 // As many as you want 14]; 15 16mocked(useAxios).mockImplementation( 17 new AxiosHooksMock(implementations).implement() 18);
This allows you to return a tuple when nothing matches, as a backup. Two ways to use it, similar to other methods.
1import AxiosHooksMock from 'axios-hooks-mock'; 2 3const refetch = jest.fn(); 4mocked(useAxios).mockImplementation( 5 new AxiosHooksMock() 6 .get('https://testapi.com', [ 7 { data: { testData: 'theData' }, loading: false }, 8 refetch 9 ]) 10 .default([{ data: undefined, loading: false }]) 11 .implement() 12);
1const default = [{ data: undefined, loading: false }]; 2mocked(useAxios).mockImplementation( 3 new AxiosHooksMock(implementations, default).implement() 4);
If you like: AxiosHooksMock.construct(implementations).implement();
This is what is returned from useAxios()
.
1type AxiosHooksTuple = [ 2 ResponseValues<unknown>, 3 ( 4 config?: AxiosRequestConfig | string, 5 options?: RefetchOptions 6 ) => AxiosPromise<unknown> 7];
Param | Required | Type |
---|---|---|
implementations | false | `{ config: AxiosRequestConfig |
Param | Required | Type |
---|---|---|
config | true | `AxiosRequestConfig |
implementation | true | AxiosHooksTuple |
Param | Required | Type |
---|---|---|
url | true | string; |
implementation | true | AxiosHooksTuple |
No Params
No. This would vastly increase the complexity of your test, including async waiting, a Provider that would need to wrap your code and intercept useAxios requests, and more. It is much easier to test each state separately.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 7/8 approved changesets -- score normalized to 8
Reason
dependency not pinned by hash detected -- score normalized to 1
Details
Reason
project is archived
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
50 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More