I am trying to mock an axios module by create this Promise function
// __mocks__/axios.js
export default function axios() {
return new Promise((resolve) =>
It looks like you are trying to mock the default export for axios
to be a mock function that returns a resolved Promise
.
In that case you can create your mock for axios
like this:
__mocks__/axios.js
export default jest.fn(() => Promise.resolve({ data: {} }));
...and you can use it in a test like this:
import axios from 'axios';
const func = () => axios();
test('func', async () => {
const promise = func();
expect(axios).toHaveBeenCalledTimes(1); // Success!
await expect(promise).resolves.toEqual({ data: {} }); // Success!
})
In my tests, I usually just mock axios like this:
import axios from "axios";
jest.mock("axios");
const mockAxios = axios as jest.Mocked<typeof axios>;
Then in your describe block:
beforeEach(() => {
mockAxios.request.mockImplementationOnce(
(): Promise<any> => Promise.resolve({ hello: "world" })
);
});