Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(jest-runtime): fall back to native ESM when a .js file contains ESM syntax#16152

Merged
SimenB merged 21 commits into
jestjs:mainfrom
SimenB:esm-fallback
May 11, 2026
Merged

fix(jest-runtime): fall back to native ESM when a .js file contains ESM syntax#16152
SimenB merged 21 commits into
jestjs:mainfrom
SimenB:esm-fallback

Conversation

@SimenB
Copy link
Copy Markdown
Member

@SimenB SimenB commented May 9, 2026

Summary

For #16144

Test plan

Tests added

@netlify
Copy link
Copy Markdown

netlify Bot commented May 9, 2026

Deploy Preview for jestjs ready!

Name Link
🔨 Latest commit b39c8e9
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/6a0192b1fc6ca30008494667
😎 Deploy Preview https://deploy-preview-16152--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

SimenB added a commit to SimenB/jest that referenced this pull request May 9, 2026
@SimenB SimenB marked this pull request as ready for review May 9, 2026 14:09
Copilot AI review requested due to automatic review settings May 9, 2026 14:09
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 9, 2026

Open in StackBlitz

babel-jest

npm i https://pkg.pr.new/babel-jest@16152

babel-plugin-jest-hoist

npm i https://pkg.pr.new/babel-plugin-jest-hoist@16152

babel-preset-jest

npm i https://pkg.pr.new/babel-preset-jest@16152

create-jest

npm i https://pkg.pr.new/create-jest@16152

@jest/diff-sequences

npm i https://pkg.pr.new/@jest/diff-sequences@16152

expect

npm i https://pkg.pr.new/expect@16152

@jest/expect-utils

npm i https://pkg.pr.new/@jest/expect-utils@16152

jest

npm i https://pkg.pr.new/jest@16152

jest-changed-files

npm i https://pkg.pr.new/jest-changed-files@16152

jest-circus

npm i https://pkg.pr.new/jest-circus@16152

jest-cli

npm i https://pkg.pr.new/jest-cli@16152

jest-config

npm i https://pkg.pr.new/jest-config@16152

@jest/console

npm i https://pkg.pr.new/@jest/console@16152

@jest/core

npm i https://pkg.pr.new/@jest/core@16152

@jest/create-cache-key-function

npm i https://pkg.pr.new/@jest/create-cache-key-function@16152

jest-diff

npm i https://pkg.pr.new/jest-diff@16152

jest-docblock

npm i https://pkg.pr.new/jest-docblock@16152

jest-each

npm i https://pkg.pr.new/jest-each@16152

@jest/environment

npm i https://pkg.pr.new/@jest/environment@16152

jest-environment-jsdom

npm i https://pkg.pr.new/jest-environment-jsdom@16152

@jest/environment-jsdom-abstract

npm i https://pkg.pr.new/@jest/environment-jsdom-abstract@16152

jest-environment-node

npm i https://pkg.pr.new/jest-environment-node@16152

@jest/expect

npm i https://pkg.pr.new/@jest/expect@16152

@jest/fake-timers

npm i https://pkg.pr.new/@jest/fake-timers@16152

@jest/get-type

npm i https://pkg.pr.new/@jest/get-type@16152

@jest/globals

npm i https://pkg.pr.new/@jest/globals@16152

jest-haste-map

npm i https://pkg.pr.new/jest-haste-map@16152

jest-jasmine2

npm i https://pkg.pr.new/jest-jasmine2@16152

jest-leak-detector

npm i https://pkg.pr.new/jest-leak-detector@16152

jest-matcher-utils

npm i https://pkg.pr.new/jest-matcher-utils@16152

jest-message-util

npm i https://pkg.pr.new/jest-message-util@16152

jest-mock

npm i https://pkg.pr.new/jest-mock@16152

@jest/pattern

npm i https://pkg.pr.new/@jest/pattern@16152

jest-phabricator

npm i https://pkg.pr.new/jest-phabricator@16152

jest-regex-util

npm i https://pkg.pr.new/jest-regex-util@16152

@jest/reporters

npm i https://pkg.pr.new/@jest/reporters@16152

jest-resolve

npm i https://pkg.pr.new/jest-resolve@16152

jest-resolve-dependencies

npm i https://pkg.pr.new/jest-resolve-dependencies@16152

jest-runner

npm i https://pkg.pr.new/jest-runner@16152

jest-runtime

npm i https://pkg.pr.new/jest-runtime@16152

@jest/schemas

npm i https://pkg.pr.new/@jest/schemas@16152

jest-snapshot

npm i https://pkg.pr.new/jest-snapshot@16152

@jest/snapshot-utils

npm i https://pkg.pr.new/@jest/snapshot-utils@16152

@jest/source-map

npm i https://pkg.pr.new/@jest/source-map@16152

@jest/test-result

npm i https://pkg.pr.new/@jest/test-result@16152

@jest/test-sequencer

npm i https://pkg.pr.new/@jest/test-sequencer@16152

@jest/transform

npm i https://pkg.pr.new/@jest/transform@16152

@jest/types

npm i https://pkg.pr.new/@jest/types@16152

jest-util

npm i https://pkg.pr.new/jest-util@16152

jest-validate

npm i https://pkg.pr.new/jest-validate@16152

jest-watcher

npm i https://pkg.pr.new/jest-watcher@16152

jest-worker

npm i https://pkg.pr.new/jest-worker@16152

pretty-format

npm i https://pkg.pr.new/pretty-format@16152

commit: b39c8e9

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates jest-runtime’s ESM/CJS interop so that when a .js file contains ESM syntax (e.g. export) but lacks an ESM marker (.mjs / "type":"module"), Jest can detect that situation and fall back to native ESM loading (addressing #16144).

Changes:

  • Introduces CjsParseError and uses es-module-lexer to detect “module syntax” on CJS parse/compile failures, enabling a native-ESM retry.
  • Updates the CJS-as-ESM synthetic-module path to return null on CjsParseError, allowing callers to fall back to loadEsmModule / requireEsm.
  • Adds regression coverage via unit tests and new E2E coverage, plus updates dependencies and changelog.

Reviewed changes

Copilot reviewed 16 out of 19 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
yarn.lock Bumps/aligns es-module-lexer resolution to support new runtime dependency.
scripts/cleanE2e.mjs Excludes the new e2e fixture’s node_modules/ from cleanup.
packages/jest-runtime/src/internals/syntheticBuilders.ts Makes CJS-as-ESM synthetic builder return null on CjsParseError to enable native-ESM fallback.
packages/jest-runtime/src/internals/ModuleExecutor.ts Adds CjsParseError and detects ESM syntax via es-module-lexer when compileFunction fails.
packages/jest-runtime/src/internals/EsmLoader.ts Uses the null synthetic-module signal to fall back to native ESM loading.
packages/jest-runtime/src/internals/CjsLoader.ts Updates require() fallback to use instanceof CjsParseError and surface original causes correctly.
packages/jest-runtime/src/internals/CjsExportsCache.ts Throws CjsParseError when CJS export parsing fails due to detected ESM syntax.
packages/jest-runtime/src/internals/tests/ModuleExecutor.test.ts Updates tests to cover CjsParseError behavior.
packages/jest-runtime/src/internals/tests/CjsExportsCache.test.ts Adds coverage asserting CjsParseError is thrown for ESM-syntax sources.
packages/jest-runtime/src/tests/test_esm_interop_root/node_modules/esm-no-marker/package.json Adds a fixture package used to reproduce “ESM syntax in .js without marker”.
packages/jest-runtime/src/tests/test_esm_interop_root/node_modules/esm-no-marker/index.js Adds the ESM-syntax .js fixture module.
packages/jest-runtime/src/tests/test_esm_interop_root/import-esm-no-marker.mjs Adds an ESM entrypoint importing from the fixture package.
packages/jest-runtime/src/tests/runtime_esm_cjs_interop.test.ts Adds integration tests covering both import and require() fallback behavior.
packages/jest-runtime/package.json Adds es-module-lexer runtime dependency.
e2e/native-esm-js-esm-syntax-fallback/package.json Adds new e2e fixture config to exercise the fallback end-to-end.
e2e/native-esm-js-esm-syntax-fallback/tests/esm-fallback.test.js Adds fixture test verifying the fallback works.
e2e/tests/nativeEsmJsEsmSyntaxFallback.test.ts Adds new e2e harness test + snapshot assertion.
e2e/tests/snapshots/nativeEsmJsEsmSyntaxFallback.test.ts.snap Snapshot for the new e2e harness test.
CHANGELOG.md Adds a main-branch fix entry for jest-runtime.

Comment thread packages/jest-runtime/src/internals/EsmLoader.ts Outdated
Comment thread packages/jest-runtime/src/internals/ModuleExecutor.ts Outdated
Comment thread packages/jest-runtime/src/internals/CjsExportsCache.ts Outdated
@SimenB SimenB requested a review from Copilot May 10, 2026 05:49
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 25 changed files in this pull request and generated 6 comments.

Comment thread packages/jest-runtime/src/internals/CjsExportsCache.ts Outdated
Comment thread packages/jest-runtime/src/internals/ModuleExecutor.ts Outdated
Comment thread packages/jest-runtime/src/internals/EsmLoader.ts
Comment thread packages/jest-runtime/src/internals/ModuleExecutor.ts
Comment thread packages/jest-runtime/src/internals/ModuleExecutor.ts Outdated
Comment thread packages/jest-runtime/src/internals/__tests__/syntheticBuilders.test.ts Outdated
SimenB and others added 3 commits May 10, 2026 08:06
…compileFunction

vm.compileFunction throws a SyntaxError from the VM realm; instanceof Error
fails cross-realm. Replace the narrowing check with jest-util's isError()
(backed by util.isNativeError / Error.isError) which works across realms.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 20 out of 26 changed files in this pull request and generated 1 comment.

Comment thread packages/jest-runtime/src/internals/EsmLoader.ts Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 31 changed files in this pull request and generated 2 comments.

Comment thread packages/jest-runtime/src/internals/EsmLoader.ts
Comment thread packages/jest-runtime/src/internals/CjsLoader.ts Outdated
@SimenB SimenB requested a review from Copilot May 11, 2026 08:02
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 22 out of 30 changed files in this pull request and generated 4 comments.

: esmError;
});
if (!(error instanceof CjsParseError)) throw error;
return this.loadEsmModule(modulePath);
Comment thread packages/jest-runtime/src/internals/ModuleExecutor.ts
Comment thread packages/jest-runtime/src/internals/esmLexer.ts
Comment thread packages/test-utils/src/ConditionalTest.ts Outdated
@SimenB SimenB changed the title fix(jest-runtime): fall back to native ESM when a .js file contains ESM syntax but lacks a type:module marker fix(jest-runtime): fall back to native ESM when a .js file contains ESM syntax May 11, 2026
@SimenB SimenB merged commit c24dcd4 into jestjs:main May 11, 2026
90 checks passed
@SimenB SimenB deleted the esm-fallback branch May 11, 2026 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants