1. 11
    Mocking out our API Helper with jest.mock
    1m 55s

Mocking out our API Helper with jest.mock

InstructorJamund Ferguson

Share this video with your friends

Send Tweet

There are several ways to mock APIs for our tests.

Here we're using jest.mock with the module factory parameter to a new mocked module to replace our original. This helps us avoid problems like servers taking a long time and node.js not having fetch built-in.

When using mocks this way it's important to note that this mocked module is only going to show up for the tests executed in this file. Other test suites will continue to receive the original implementation of app/api.

Other approaches to mock APIs include mocking out window.fetch or using Mock Service Worker to provide a more robust API implementation. However, the approach we took here makes sense for our use case. We have a simple API surface, it lives in a single file, we never call fetch directly and we don't need a lot of bells and whistles (like responding to different routes or query parameters). So for that reason this approach and the spyOn approach used in some of the later lessons actually works really well.

If you're using Apollo or ReactQuery or SWR to handle your data, then you almost certainly should use Mock Service Worker instead of the jest.mock approach.