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
npm install axios-hooks-mock
56.1
Supply Chain
97.2
Quality
74.7
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
21 Commits
3 Watching
5 Branches
1 Contributors
Updated on 28 Jan 2023
Minified
Minified + Gzipped
TypeScript (98.11%)
JavaScript (1.89%)
Cumulative downloads
Total Downloads
Last day
-17.8%
60
Compared to previous day
Last week
-9%
363
Compared to previous week
Last month
-4.9%
1,851
Compared to previous month
Last year
-36.8%
25,010
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
47 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
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