如何通过Jest模拟解决Flow类型错误

Pal*_*and 6 javascript jestjs flowtype

我正在使用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(...)
});
Run Code Online (Sandbox Code Playgroud)

如何阻止Flow报告property 'mockImplementation' not found in statics of function错误而不进行错误抑制(例如$FlowFixMe)?

据我所知,这个问题来自于一个事实,即模块中定义的功能不是玩笑,戏弄功能,并且尽可能流程而言,不包含方法,如mockImplementationmockReset等。

Pal*_*and 7

谢谢Andrew Haines,您对相关问题发表的评论提供了解决方案。我对以下内容感到满意:

const mock = (mockFn: any) => mockFn;

test("something when funcOne returns 'foo'", () => {
    mock(funcOne).mockImplementation(() => 'foo');  // mo more flow errors!
    ...
});
Run Code Online (Sandbox Code Playgroud)