Home

Awesome

Jest: How do I mock x?

This repo contains runnable minimal reproducable examples of where I've been stuck in the past trying to work out how to mock something, and (maybe) the solution.

The aim of this repo is to:

Feel free to PR with any other jest testing puzzles you've faced!

Scenarios

Running the tests

$ yarn # install node_modules
$ yarn test # runs Jest

Further reading / Prior art

Testing approach

There are many ways to unit test, and I'm not suggesting what's "good" or "bad". This section just provides context as to why the solutions here may or may not look idiosyncratic.

Unit testing setup

For the most part, test files usually:

I personally don't usually define manual mock files for modules, and prefer to inline the mocking where possible. This is because: