且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法读取未定义的属性“mockResolvedValue"

更新时间:1970-01-01 07:57:30

既然你已经mock过axios类,那么mock axios.get返回值的方法之一就是这样做:

Since you have already mocked the axios class, one of the ways of mocking the return value of axios.get is to do this:

axios.get = jest.fn().mockResolvedValue({
  data: [
    {
      userId: 1,
      id: 1,
      title: 'test'
    }
  ]
});
.
.
expect(axios.get).toHaveBeenCalledTimes(1);

或者,您可以监视 axios.get(),并提供模拟返回值:

Alternatively, you can spy on axios.get(), and provide a mocked return value:

jest.spyOn(axios, 'get').mockResolvedValueOnce({
  data: [
    {
      userId: 1,
      id: 1,
      title: 'test'
    }
  ]
});