0

如何在我的笑话测试中模拟 axios 调用?我正在这样做

jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;

test('something', () => {
...

  mockedAxios.get.mockReturnValueOnce({ data: [] });
});

我在下面不断收到此错误:

错误 TS2345:类型参数 '{ data: undefined[]; }' 不能分配给类型为 '(url: string, config?: AxiosRequestConfig) => AxiosPromise' 的参数。对象字面量只能指定已知属性,并且类型 '(url: string, config?: AxiosRequestConfig) => AxiosPromise' 中不存在 'data'。

4

1 回答 1

0

嘿,茉莉花,你能试试这个吗?

import axios from 'axios';
jest.mock('axios');

test('something', () => {
...
  const mockGet = jest.spyOn(axios, 'get');
  mockGet.mockResolvedValue({ data: [] });
});
于 2019-08-01T22:04:38.607 回答