当想用 Jest 模拟外部模块时,我们可以使用 jest.mock()
方法来自动模拟模块上的函数。
然后,我们可以根据需要操作和询问模拟模块上的模拟函数。
例如,考虑以下模拟 axios 模块的人为示例:
import myModuleThatCallsAxios from '../myModule';
import axios from 'axios';
jest.mock('axios');
it('Calls the GET method as expected', async () => {
const expectedResult: string = 'result';
axios.get.mockReturnValueOnce({ data: expectedResult });
const result = await myModuleThatCallsAxios.makeGetRequest();
expect(axios.get).toHaveBeenCalled();
expect(result).toBe(expectedResult);
});
以上将在 Jest 中运行良好,但会引发 Typescript 错误:
类型 ‘(url: string, config?: AxiosRequestConfig | undefined) => AxiosPromise’ 上不存在属性 ‘mockReturnValueOnce’。
axios.get
的 typedef 正确地不包括 mockReturnValueOnce
属性。我们可以通过将 axios.get
包装为 Object(axios.get)
来强制 Typescript 将其视为对象文字,但是:
在保持类型安全的同时模拟函数的惯用方法是什么?
原文由 duncanhall 发布,翻译遵循 CC BY-SA 4.0 许可协议
从 ts-jest 27.0
mocked
fromts-jest
开始,将在 28.0 中弃用和删除,您可以在官方 文档 中查看。所以请改用mocked
来自jest
。这是 文档所以对于你的例子: