/understanding-jest-mocks-typescript

`jest.mock` confusion in TypeScript

Primary LanguageTypeScriptMIT LicenseMIT

Understanding Jest mocks in TypeScript

Why

I created this repository because I was totally confused about how to use jest.mock() with TypeScript.

Confusions

First, I struggled with the official guide about how to use TypeScript in jest, which involved installing lots of babel packages and configuring it inside your project.

The next struggle was about how to access properties of the mock object from inside the test without type errors. One thing that worked was telling TypeScript to shut up via //@ts-ignore. But that didn't feel quite right.

The solution was to use ts-jest and the mocked utility function from ts-jest/utils as described in this Github issue comment. Saved my day :).

The program to be tested

This program consumes the people/:id endpoint from the Star Wars API and logs the output. The test tests the implementation of the api function getPeople. The fetch call in the test is mocked.