I initially found a solution on this StackOverflow thread
However, I wanted to add my own helper function that generates a fresh Axios instance with the user's authentication token.
Here is what I came up with:
import axios from "axios";
const mockAxios: jest.Mocked<typeof axios> = jest.createMockFromModule("axios");
// To resolve the axios.create() undefined error!
mockAxios.create = jest.fn(() => {
return mockAxios;
});
export const createAuthenticatedInstance = () => {
return mockAxios.create();
};
export default mockAxios;
Why is mockAxios.create() returning undefined?
Even though the 'mockAxios' object and the create function are defined, calling create returns undefined instead of the expected new instance. I could work around this by simply returning mockAxios, but I'm curious about why it doesn't function as intended from the start. My expectation was to get an identical, new instance similar to mockAxios, but it consistently returns undefined.