I am facing an issue where I have defined an interface that requires certain functions to be implemented by objects. For instance:
interface MyInterface {
someFunc: () => void,
/* ... other details */
}
Now, I need to create a test to ensure that these functions are called under specific conditions.
To achieve this, I utilized test fakes in the following manner:
const obj: MyInterface = {
...fakeObj,
someFunc: jest.fn(() => {})
};
/* ... actual testing code */
expect(obj.someFunc.mock.calls.length).toBe(1);
Initially, everything was working fine until I explicitly assigned a type to the obj
variable. This change resulted in the error message:
Error: Property 'mock' does not exist on type '() => void'.
My intention behind adding types to my objects was to enable smooth usage of refactoring tools without the hassle of manually correcting all tests.
How can I effectively write a test to verify function calls while implementing explicit types?