最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to resolve Flow type error from Jest mocking - Stack Overflow

programmeradmin6浏览0评论

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(...)
});

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

I understand that the issue es 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.

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(...)
});

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

I understand that the issue es 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.

Share Improve this question asked Jul 11, 2017 at 1:17 PalisandPalisand 1,3621 gold badge18 silver badges35 bronze badges 1
  • Related issue: github./flowtype/flow-typed/issues/291 – Andrew Haines Commented Jul 31, 2017 at 8:13
Add a ment  | 

3 Answers 3

Reset to default 10

Thanks, Andrew Haines, the ments 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!
    ...
});

Instead of suppressing the error with any I remend to use JestMockFn type. Here is a related issue: https://github./flow-typed/flow-typed/issues/291

Example (copied from the link above):

import ajax from '../../js/m/ajax';
jest.mock('../../js/m/ajax', () => {
  return {
    default: jest.fn(),
  }
});
const mockAjax: JestMockFn<[string], Promise<{body: {}}>> = ajax;
describe('ConfigurationProvider', () => {
  it('calling the fetchConfig() should return a promise', () => {
    const expectedCfg = {x:'y'};
    mockAjax.mockReturnValueOnce(
      Promise.resolve({body:expectedCfg})
    );
    ...
  });
});

Here is how the type is defined in the latest jest version: https://github./flow-typed/flow-typed/blob/master/definitions/npm/jest_v25.x.x/flow_v0.104.x-/jest_v25.x.x.js

Note that the type is global and doesn't have to be imported (which is unfortunate decision in my opinion but that is a different topic).

You can loosen the type constraint inline as well:

test("something when funcOne returns 'foo'", () => {
    (funcOne: any).mockImplementation(() => 'foo');  // mo more flow errors!
    ...
});
发布评论

评论列表(0)

  1. 暂无评论