我正在使用 Jest 模拟模块中的某些功能并以下列方式进行测试:
jest.mock("module", () => ({
funcOne: jest.fn(),
funcTwo: jest.fn(),
...
}));
import {funcOne, funcTwo, ...} from "module";
test("something when funcOne returns 'foo'", () => {
funcOne.mockImplementation(() => 'foo'); // <- Flow error
expect(...)
});
test("that same thing when funcOne returns 'bar'", () => {
funcOne.mockImplementation(() => 'bar'); // <- Flow error
expect(...)
});
如何阻止 Flow 报告 property 'mockImplementation' not found in statics of function
错误没有错误抑制(例如 $FlowFixMe
)?
我知道这个问题是因为模块中定义的函数不是 Jest 模拟函数,并且就 Flow 而言,不包含 mockImplementation
、mockReset
等
最佳答案
谢谢 Andrew Haines,对 related issue 的评论您发布的内容提供了解决方案。我对以下内容感到满意:
const mock = (mockFn: any) => mockFn;
test("something when funcOne returns 'foo'", () => {
mock(funcOne).mockImplementation(() => 'foo'); // mo more flow errors!
...
});
关于javascript - 如何解决 Jest 模拟中的 Flow 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45023871/