-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to mock axios response if we use axios-hooks? #35
Comments
If you're using this library don't mock axios, mock this library via jest |
Thanks. I'll try to mock axios-hooks response array. |
@LiaoJimmy , how does your code looks after mocking |
I think mock |
@LiaoJimmy @lmiguelvargasf @tgfischer mocking this library with jest is as easy as: import useAxios from 'axios-hooks'
jest.mock('axios-hooks')
// in your test
useAxios.mockReturnValue([{ data: { ... } }]) |
@simoneb how would you go about mocking axios-hooks when you have multiple instances of useAxios within a file?
|
@KristianLonergan in the same way as you would mock a function which is called multiple times, see here. I appreciate this is not ideal so I'll be looking into #218 that's been opened a few days ago. But, there are ways to mock this easily already. |
Just to throw out there, and it is not mutally exclusive with #218 ... I really appreciate this library, and I had some more complicated mocking scenarios that required several useAxios hooks to be used with each other, relying on one another. The ordered mocking method above falls down when firing off items in random order and such. So, I created a library to handle every scenario I (and the rest of my company) needed, and you all are free to use it as well: https://www.npmjs.com/package/axios-hooks-mock It's an implementation that should work well for both simple and complex scenarios and abstracts most of the testing boilerplate away. Tagging @simoneb just to let him know. |
Hello, I try to mock axios response by:
@react-mock/xhr: https://github.com/ctimmerm/axios-mock-adapter
axios-mock-adapter: https://github.com/ctimmerm/axios-mock-adapter
and enve jest mock: https://jestjs.io/docs/en/mock-functions#mocking-modules
They all mock axios response failed.
Do you have any example to mock axios if we use axios-hooks?
My example code:
And here is the test file.
The text was updated successfully, but these errors were encountered: