Jest/Saga mock testing cheat sheet
Published on
We use jest combined with React testing library to test our frontend components, and redux-saga-test-plan to test sagas. 🥳
When mocking things in tests, be it an API call, saga generator functions or Atlaskit modules. I always seem to forget which mock is the right one for the current use case. Given this problem, I’ve made it easier for myself by mocking up this cheatsheet with a 1–2 line explanation about the mock. Hope it helps you too!
Use case: Mock the implementation of a function defined in another file.
1. Using MockImplementation()
The function being imported needs to be marked as a jest.fn()
before we can call mockImplementation
on it.
2. Using Jest.mock()
Note: : But wait there is another way! If all you need is the return value of the promise but don't need to touch the implementation, you can use mockResolvedValue()
. mockResolvedValue
is synctatic sugar for mockImplementation()
and only returns the resolved value of the mocked function!
Use case: Mocking modules.
Let's use mocking requests to Bugsnag as an example. Manual mocks are defined by writing a module in a __mocks__/ subdirectory
immediately adjacent to the module. If the module you are mocking is a node module, the mock should be placed in the __mocks__ directory
adjacent to node_modules
.
In the __mocks__
folder, define a file named bugsnag.js
and then the following code block defines mocks for bugsnag.notify()
and bugsnag.leaveBreadcrumbs()
.
Use case: Mocking Atlaskit modules.
In a similar fashion to the above, we can also mock Atlaskit modules.
In this case, we are mocking out the @atlaskit/modal-dialog
component due to the test having an extremely long run time.
Note:: When using the factory parameter for an ES6 module with a default export, the __esModule: true
property needs to be specified.
Use-case: Mocking Redux Sagas.
In Redux Sagas, you can mock function calls in the .provide
block.
You can mock a function one of 2 ways,
- Using
call()
. To usecall()
, you need to provide arguments that the function accepts.
- Using
call.fn()
. This is great to use if you only want to mock the return value and not the arguments.
A great tip when wanting to mock behaviour in sagas is to look at the matchers documentation.
I was able to mock a tricky race condition using matchers.race(effects)
😅.
This is a living document and I will be adding to it when I run into more mocks I find confusing. If I’ve got something wrong, I’m all ears!
Happy mocking!!