When testing a module that has a dependency in a different file. When assigning that module to be jest.Mock
typescript gives an error that the method mockReturnThisOnce
(or any other jest.Mock method) does not exist on the dependency, this is because it is previously typed. What is the proper way to get typescript to inherit the types from jest.Mock?
Here is a quick example.
Dependency
const myDep = (name: string) => name;
export default myDep;
test.ts
import * as dep from '../depenendency';
jest.mock('../dependency');
it('should do what I need', () => {
//this throws ts error
// Property mockReturnValueOnce does not exist on type (name: string)....
dep.default.mockReturnValueOnce('return')
}
I feel like this is a very common use case and not sure how to properly type this. Any help would be much appreciated!
import
are evaluated first, no matter if you put some code before the import. So this won't work. – mgolmock...
– Tobi