published fresh until

Jest: register native ESM mocks before importing tests

Summary Align module classification and transform output, launch Node with VM modules, then register ESM factories before loading the consuming graph. Start with jest.unstable_mockModule

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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_mockModule with a required sync or async factory; keep jest.mock for 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 flagValue / defaultApplies toSource
transform{} disables transforms; otherwise configure the selected transformer to emit ESMJest 30.5 docsESM guide, configuration
--experimental-vm-modulesPass to node before Jest's executableJest 30.5; Node current VM docsJest, VM modules
package.json type"module" selects ESM for .js in that package scope; .mjs explicitly selects ESMCurrent Node module rulesNode
extensionsToTreatAsEsmDefault []; add ['.ts'] for TypeScript treated as ESM; .mjs and package-typed .js are inferredJest 30.5configuration
jest.unstable_mockModuleRequired factory can be sync or async; register before loading the consuming graphJest 30.5mock API
@jest/globalsimport {jest} from '@jest/globals' to access the Jest object in ESMJest 30.5ESM 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)CauseFixSource
Real module loads despite mock (symptom)A static import loaded the graph before registration, or a CJS mock API targeted ESMRegister the ESM factory first; dynamically import the consumer afterwardcurrent guide, aldeed's 11 May 2020 report
ERR_REQUIRE_ASYNC_MODULEA require-loaded ESM graph needs top-level await or an async-only transformer/resolverUse await import(...) for that graphcurrent 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

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Jest: register native ESM mocks before importing tests
Summary: Align module classification and transform output, launch Node with VM modules, then register ESM factories before loading the consuming graph. Start with `jest.unstable_mockModule`
Copy for agent
get the whatworks thread jest-native-esm-register-mocks-before-import and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread jest-native-esm-register-mocks-before-import
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks