question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

typing jest mocks in tests

See original GitHub issue

Extract of a jest test:

jest.mock('../../js/comm/ajax');
import ajax from '../../js/comm/ajax';
describe('ConfigurationProvider', () => {
  it('calling the fetchConfig() should return a promise', () => {
    const expectedCfg = {x:'y'};
    ajax.mockReturnValueOnce(
      Promise.resolve({body:expectedCfg})
    );
    ...
  });
});

The original ajax module exported a function so jest will intercept and return a JestMockFn instead. Flow says property mockReturnValueOnce not found in statics of function. How do I let flow know of this? Should I cast the ajax symbol to the JestMockFn type? And how?

Thanks 😃

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:10
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
jeffmocommented, Oct 19, 2016

Yea, the wrapper-function is what i did in the flow-typed CLI:

https://github.com/flowtype/flow-typed/blob/master/cli/src/lib/__tests__/libDefs-test.js#L30-L32

I used any there, but it would probably be better to have the jest libdef export the interface of a mock and just have it cast to that:

import type {JestMockT} from "jest";
function _mock(mockFn) {
  return ((mockFn: any): JestMockT);
}
2reactions
aaronjensencommented, Oct 18, 2016

Perhaps adding a utility function would be handy:

const mock = (mockObject: any): MockObject => mockObject

mock(ajax).mockReturnValueOnce(...)

You could even potentially make that function polymorphic and strongly type things like mockReturnValueOnce.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to strongly type jest mocks - Stack Overflow
There is a library that can help you with strongly typed mocks in Typescript with Jest: jest-mock-extended.
Read more >
Mock Functions - Jest
The jest.Mocked<Source> utility type returns the Source type wrapped with type definitions of Jest mock function. ... });. Types of classes, ...
Read more >
Typed mocks for Jest - HipsterBrown
jest -when allows you to use a set of the original Jest mock functions in order to train your mocks only based on...
Read more >
Understanding Jest Mocks - Medium
Mocking is a technique to isolate test subjects by replacing dependencies with objects that you can control and inspect.
Read more >
How to use TypeScript and Jest mocks - Breno Calazans
It's pretty common to mock modules in Jest. ... And finally, create our test file using mocked foo : 1// index.test.ts.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found