What this thread does
Do first: run node --experimental-vm-modules node_modules/jest/bin/jest.js with ESM output, then register jest.unstable_mockModule before dynamically importing the subject.
Short answer
As of November 2026, Jest 30.5.2 retains experimental native ESM support. Disable transforms or ensure the transformer emits ESM. Static imports execute before the test body's mock registration, so dynamically import both the mocked target and modules consuming it after registration (ESM guide).
Choose / don't choose
- Native JavaScript ESM: choose
transform: {}when the source needs no compilation; don't disable a transform that the runtime still needs for that source language (activation). - Transformed source: choose an ESM-emitting transformer and an ESM extension policy; don't assume CommonJS output is native ESM (ESM transform guidance).
- Mocked ESM: choose
jest.unstable_mockModulewith a required sync or async factory; keepjest.mockfor resolved CJS targets. On Node 24.9+, the current guide also supports automocks/manual mocks for synchronously evaluable ESM graphs, for async graphs, use the explicit factory path (current boundary).
Not for: missing DOM globals in Jest; see jest-document-window-not-defined-dom-test-environment.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
transform | {} disables transforms; otherwise configure the selected transformer to emit ESM | Jest 30.5 docs | ESM guide, configuration |
--experimental-vm-modules | Pass to node before Jest's executable | Jest 30.5; Node current VM docs | Jest, VM modules |
package.json type | "module" selects ESM for .js in that package scope; .mjs explicitly selects ESM | Current Node module rules | Node |
extensionsToTreatAsEsm | Default []; add ['.ts'] for TypeScript treated as ESM; .mjs and package-typed .js are inferred | Jest 30.5 | configuration |
jest.unstable_mockModule | Required factory can be sync or async; register before loading the consuming graph | Jest 30.5 | mock API |
@jest/globals | import {jest} from '@jest/globals' to access the Jest object in ESM | Jest 30.5 | ESM globals |
Order of operations
1. Align package/file classification and transform output. Confirm that the executed Node command includes the VM-modules flag. 2. Import the Jest object, then register the factory. Avoid earlier imports of the consuming module from the test or setup files. 3. Await a dynamic import of the subject, then execute assertions against the mock. For example, this adapts the documented child-process mock:
import {jest, expect, test} from '@jest/globals';
const execSync = jest.fn();
jest.unstable_mockModule('node:child_process', () => ({execSync}));
const childProcess = await import('node:child_process');
test('uses the mock', () => {
expect(childProcess.execSync).toBe(execSync);
});
Register the mock before loading modules which load the mocked modules (example).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
| Real module loads despite mock (symptom) | A static import loaded the graph before registration, or a CJS mock API targeted ESM | Register the ESM factory first; dynamically import the consumer afterward | current guide, aldeed's 11 May 2020 report |
ERR_REQUIRE_ASYNC_MODULE | A require-loaded ESM graph needs top-level await or an async-only transformer/resolver | Use await import(...) for that graph | current compatibility boundary, Jest ESM guide |
The historical report describes the failure of a CJS-style mock against ESM; it does not establish that the current explicit mock API is absent.
Missing
- No transformer package or source syntax is specified; its own ESM-output options must be selected from that transformer's documentation.
Checked
2026-11-12; Jest 30.5.2 release, current ESM/configuration references, Node ESM/VM references and aldeed's dated report read. No Jest commands executed.
Started from a post by @Jest contributors, packaged by the editor. Original post