
Security News
Feross on Risky Business Weekly Podcast: npm’s Ongoing Supply Chain Attacks
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
axios-hooks-mock
Advanced tools
A library that simplifies mocking for axios-hooks, especially when multiple hooks are used together.
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.
jest.mock('axios-hooks');
Having the library do this would make it far less flexible.
import AxiosHooksMock from 'axios-hooks-mock';
const refetch = jest.fn();
mocked(useAxios).mockImplementation(
new AxiosHooksMock()
.get('https://testapi.com', [
{ data: { testData: 'theData' }, loading: false },
refetch
])
// Chain as many as you want
.implement()
);
import AxiosHooksMock from 'axios-hooks-mock';
const refetch = jest.fn();
mocked(useAxios).mockImplementation(
new AxiosHooksMock()
.addImplementation({ url: 'https://testapi.com', method: 'GET' }, [
{ data: { testData: 'theData' }, loading: false },
refetch
])
// Chain as many as you want
.implement()
);
This allows for a configuration array to be handed into the constructor like so:
import AxiosHooksMock from 'axios-hooks-mock';
const refetch = jest.fn();
const implementations: AxiosHooksMockItem[] = [
{
config: { url: 'https://testapi.com', method: 'GET' },
implementation: [
{ data: { testData: 'theData' }, loading: false },
refetch
]
}
// As many as you want
];
mocked(useAxios).mockImplementation(
new AxiosHooksMock(implementations).implement()
);
This allows you to return a tuple when nothing matches, as a backup. Two ways to use it, similar to other methods.
import AxiosHooksMock from 'axios-hooks-mock';
const refetch = jest.fn();
mocked(useAxios).mockImplementation(
new AxiosHooksMock()
.get('https://testapi.com', [
{ data: { testData: 'theData' }, loading: false },
refetch
])
.default([{ data: undefined, loading: false }])
.implement()
);
const default = [{ data: undefined, loading: false }];
mocked(useAxios).mockImplementation(
new AxiosHooksMock(implementations, default).implement()
);
If you like: AxiosHooksMock.construct(implementations).implement();
This is what is returned from useAxios()
.
type AxiosHooksTuple = [
ResponseValues<unknown>,
(
config?: AxiosRequestConfig | string,
options?: RefetchOptions
) => AxiosPromise<unknown>
];
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.
FAQs
A library that simplifies mocking for axios-hooks, especially when multiple hooks are used together.
We found that axios-hooks-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.