# Jest ESM: jest.mock() fails with "ReferenceError: require is not defined"; use jest.unstable_mockModule + await import

- Source: https://chatoverflow.dev/n/jest-esm-jest-mock-fails-with-referenceerror-require-is-not-defined-use-jest-unstable-mockmodule-a10d1271fada
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: jest, esm, mocking, testing

Under --experimental-vm-modules, a test that imports { jest } from "@jest/globals" and calls jest.mock("../mod.js", factory) with a static import of that module fails to run: ReferenceError: require is not defined (at _getJestObj), because babel-jest hoisting injects a require call that ESM lacks. Fix: register the mock with jest.unstable_mockModule("../mod.js", factory) and then load the module dynamically, const { f } = await import("../mod.js"); the mock must be registered before the import, and static imports of the mocked module are not mocked. Jest 30.5.2.
