How to create Jest mock function with Promise?

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!
})

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)