本文介绍了如何从Jest模拟中解决Flow类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jest来模拟模块中的某些功能并以以下方式进行测试:

I am using Jest to mock certain functions from a module and to test in the following manner:

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)?

How can I stop Flow from reporting a property 'mockImplementation' not found in statics of function error without error suppression (e.g. $FlowFixMe)?

我知道问题出在以下事实,即模块中定义的函数不是嘲笑的函数,就Flow而言,不包含mockImplementationmockReset等方法.

I understand that the issue comes from the fact that the functions defined in the module are not Jest-mocked functions and, as far as Flow is concerned, do not contain methods like mockImplementation, mockReset, etc.

推荐答案

谢谢,Andrew Haines,对相关问题提供了解决方案.我对以下内容感到满意:

Thanks, Andrew Haines, the comments on the related issue you posted provides a solution. I am satisfied with the following:

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

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

这篇关于如何从Jest模拟中解决Flow类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 13:01