Bypassing module mocks

    Jest allows you to mock out whole modules in your tests, which can be useful for testing if your code is calling functions from that module correctly. However, sometimes you may want to use parts of a mocked module in your test file, in which case you want to access the original implementation, rather than a mocked version. However, sometimes you may want to use parts of a mocked module in your test file, in which case you want to access the original implementation, rather than a mocked version.

    createUser.js

    However, if you ran that test you would find that the createUser function would fail, throwing the error: TypeError: response.text is not a function. However, if you ran that test you would find that the createUser function would fail, throwing the error: TypeError: response.text is not a function. This is because the class you’ve imported from node-fetch has been mocked (due to the jest.mock call at the top of the test file) so it no longer behaves the way it should.

    This allows your test file to import the actual Response object from , rather than a mocked version. This means the test will now pass correctly. This means the test will now pass correctly.