您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Typescript和Jest:避免在模拟函数上键入错误

Typescript和Jest:避免在模拟函数上键入错误

添加此行代码const mockedAxios = axios as jest.Mocked<typeof axios>。然后使用mockedAxios调用mockReturnValueOnce。使用您的代码,应该像这样完成:

import myModuleThatCallsAxios from '../myModule';
import axios from 'axios';

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

it('Calls the GET method as expected', async () => {
  const expectedResult: string = 'result';

  mockedAxios.get.mockReturnValueOnce({ data: expectedResult });
  const result = await myModuleThatCallsAxios.makeGetRequest();

  expect(mockedAxios.get).toHaveBeenCalled();
  expect(result).toBe(expectedResult);
});
其他 2022/1/1 18:18:25 有559人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶