TIL how to easily node_modules in unit tests using the jest testing framework from Facebook.
At work, we have been implementing client-side error logging using the service sentry.io.
raven-js which provides a few methods for easily pushing error messages to the service.
Unfortunately, after implementing logging throughout our code using the Raven package, our unit tests began to fail…
Raven was throwing errors as it was attempting to push errors to sentry from the test environment.
Clearly this is something that we should be mocking for testing purposes, but what is the easiest way to mock a node_module using
Jest recommends adding a
__mocks__ folder to the root of your repository, and keeping mocks for node_modules there.
By default, when jest detects a node_module import during tests, it will automatically look in the
__mocks__ folder for a mock that matches the name of the module being imported, and preferentially use the mock over the actual implementation.
In our case of mocking the Raven library for tests, we created our mock as follows:
jest.genMockFromModule method automocks the provided module by substituting its public methods with
jest.fn() implementations. You can still confirm in your unit tests that the correct methods were called using
You can extend the mock with additional fake implementation as necessary since it is just a regular ol’ jest
I found this to be the quickest way to mock the new node_module throughout my unit tests, while touching the fewest number of files possible.