Mocking API calls with Jest

耗尽温柔 提交于 2020-12-15 03:35:16

问题


I am trying to mock axios module inside my test file like this.

import axios from 'axios';

jest.mock('axios', () => ({
  __esModule: true,
  create: jest.fn(),
  get: jest.fn(),
  post: jest.fn(),
  default:jest.fn()
}));
 
jest.mock("../../utils/api");

Here is mocked API in a folder __mocks__

//../../utils/api

const mockLogin = jest.fn((email, password) => {
    return Promise.resolve({ email, password });
  });
   
  const errors = jest.fn((errors) =>{
     return errors
  });

  module.exports =
    {
    ...jest.requireActual("../../utils/api"),
    __esModule: true,
    mockLogin,
    post: mockLogin,
    catchErrors:errors
  }

When I run my test I get the following error.

What is wrong with my code?

This is not duplicate, because the solution provided does not solve the problem whatsoever

来源:https://stackoverflow.com/questions/64961204/mocking-api-calls-with-jest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!