How to resolve Flow type error from Jest mocking

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

Example (copied from the link above):

import ajax from '../../js/comm/ajax';
jest.mock('../../js/comm/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.com/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).


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