Mocking module method in Jest while keeping original TypeScript typing
For some time I was ignoring PhpStorm’s warning about missing types for the uuid npm module. Today I decided to use a quick-action to install @types/uuid.

It made IDE happy but caused type issues for the following (simplified) code in tests:
1 | import { v4 } from "uuid"; |
TypeScript was throwing error:
1 | error TS2339: Property 'mockReturnValue' does not exist on type 'v4'. |
I found https://stackoverflow.com/a/60300568/1050577 which is a great answer for this issue:
1 | (v4 as jest.MockedFunction<typeof v4>).mockReturnValue("123"); |
I like that I no longer see the warning in IDE. But even better, TypeScript noticed that mocking uuid.v4 return value as a number is not compatible with its original implementation. Fixing this made the tests closer to the reality, which in turn made the app more robust.