Jest mock module not working? double check these

December 15, 2022

Jest mock module feature can be used for mocking whole or part of packages in your project. Check out:

If you try jest.mock('my-package') first time, it may not work, and it just used the actual package without giving a hint of what’s wrong.

Here is three typical mistakes cause that:

jest.mock must be called at global scope, if you wrap it in a function and then call it, it won’t work.

// Wrong
function mockMe() {
// Correct

Forgot to use __esModule: true setting

If your packages are written in ESM syntax, make sure __esModule: true setting is included

jest.mock('../myModule', () => {
  // Require the original module to not be mocked...
  const originalModule = jest.requireActual('../myModule');

  return {
    __esModule: true, // Use it when dealing with esModules
    getRandom: jest.fn(() => 10),

Double check the object you’re mocking is really in the package

Take the code snippet above as an example, make sure ‘getRandom’ is actually exported from ../myModule rather than somewhere else.