Mocking and Spying with Jest and TypeScript

I have found that the easiest way to mock TypeScript objects using Jest is by simply doing this: const obj = { prop1: "some string prop", prop2: 50 } as any as MyType Now we can use obj anywhere MyType was expected and can use jest.spyOn(obj, ...) to the heart’s content. Frameworks like jest-mock-extended do similar work but I find that they’re an unnecessary abstraction that prevent the test from being expressive and obvious....

April 22, 2021