typing jest mocks in tests
See original GitHub issueExtract 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:
- Created 7 years ago
- Reactions:10
- Comments:8 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 thejest
libdef export the interface of a mock and just have it cast to that:Perhaps adding a utility function would be handy:
You could even potentially make that function polymorphic and strongly type things like
mockReturnValueOnce
.