useMutation Testing
I am attempting to write some tests for an application that we have written and I have started running into issues around mocking certain pieces.
I am attempting to test a very basic useMutation hook that we have. Internally, it is using Axios to make the HTTP requests. The simple reason for this is that we have an axios instance that uses a request interceptor to attach our Auth header to every outgoing request using that instance.
The issue at hand, is that when running the test, it is attempting to fetch a real auth token, so I think I need to mock the auth token in this situation since it really doesnt matter what it is. I cannot figure out how to do this correctly.
This interceptor is the only thing that axios is doing, so I was trying to find a way to either prevent the interceptor from running or mocking the axios instance all together.
We are authenticating with MSAL for Azure AD, so when there is no token, it make a request to the server for one.
This interceptor is the only thing that axios is doing, so I was trying to find a way to either prevent the interceptor from running or mocking the axios instance all together.
We are authenticating with MSAL for Azure AD, so when there is no token, it make a request to the server for one.
1 Reply
conscious-sapphire•3y ago
If I were you I'd reach for msw, you can intercept the auth token fetch and provide a response with a fake token for testing purposes.
https://mswjs.io/docs/api/setup-server#examples