Skip to content

fix(jest-runtime, @jest/transform): improve ESM error handling on Node < 24.9 - #16244

Merged
SimenB merged 1 commit into
jestjs:mainfrom
jonkoops:fix/esm-transform-fallback-node-lt-24.9-v2
Jun 24, 2026
Merged

SimenB merged 1 commit into
jestjs:mainfrom
jonkoops:fix/esm-transform-fallback-node-lt-24.9-v2

Conversation

@jonkoops

Copy link
Copy Markdown
Contributor

Summary

Jest 30.4's ESM runtime rewrite gates require() of ESM-marked files ("type": "module" or .mjs) behind supportsSyncEvaluate, which requires Node 24.9+. On older Node, this immediately throws ERR_REQUIRE_ESM, bypassing the transform pipeline entirely. This breaks projects that rely on a configured transform (e.g. babel-jest) to convert ESM dependencies to CJS (backstage/backstage#34182).

This PR:

  • Moves the supportsSyncEvaluate check into the outer condition so ESM-marked files fall through to the CJS path on older Node. If the configured transform converts the file, require() succeeds.
  • When no transform covers the file and ESM syntax remains after transformation, surfaces a specific ERR_REQUIRE_ESM error with actionable guidance (configure a transform or upgrade to Node v24.9+), replacing the generic "Jest encountered an unexpected token" message.
  • Removes the now-redundant ECMAScript Modules bullet from enhanceUnexpectedTokenMessage in @jest/transform, since all valid ESM cases are intercepted by the new ERR_REQUIRE_ESM error.

Cleaner reimplementation of #16240.

Test plan

Unit and e2e tests added covering:

  • Native ESM require on Node 24.9+ (testWithSyncEsm)
  • Transform fallback on older Node (testWithoutSyncEsm)
  • ERR_REQUIRE_ESM for untransformed ESM-marked packages (testWithoutSyncEsm)
  • Genuine syntax errors pass through unchanged (testWithoutSyncEsm)
  • e2e: ERR_REQUIRE_ESM for untranspiled node_modules with ESM syntax

@netlify

netlify Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit bc8e8e7
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/6a3b04c3d67b8b0008233e28
😎 Deploy Preview https://deploy-preview-16244--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.

@github-actions github-actions Bot added the require-changelog If a PR does requires a changelog entry label Jun 17, 2026
@jonkoops
jonkoops force-pushed the fix/esm-transform-fallback-node-lt-24.9-v2 branch from ebcf055 to 6f056f4 Compare June 17, 2026 16:37
@pkg-pr-new

pkg-pr-new Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

babel-jest

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

babel-plugin-jest-hoist

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

babel-preset-jest

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

create-jest

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

@jest/diff-sequences

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

expect

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

@jest/expect-utils

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

jest

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

jest-changed-files

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

jest-circus

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

jest-cli

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

jest-config

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

@jest/console

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

@jest/core

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

@jest/create-cache-key-function

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

jest-diff

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

jest-docblock

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

jest-each

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

@jest/environment

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

jest-environment-jsdom

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

@jest/environment-jsdom-abstract

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

jest-environment-node

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

@jest/expect

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

@jest/fake-timers

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

@jest/get-type

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

@jest/globals

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

jest-haste-map

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

jest-jasmine2

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

jest-leak-detector

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

jest-matcher-utils

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

jest-message-util

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

jest-mock

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

@jest/pattern

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

jest-phabricator

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

jest-regex-util

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

@jest/reporters

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

jest-resolve

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

jest-resolve-dependencies

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

jest-runner

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

jest-runtime

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

@jest/schemas

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

jest-snapshot

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

@jest/snapshot-utils

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

@jest/source-map

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

@jest/test-result

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

@jest/test-sequencer

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

@jest/transform

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

@jest/types

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

jest-util

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

jest-validate

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

jest-watcher

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

jest-worker

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

pretty-format

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

commit: bc8e8e7

@SimenB SimenB left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks great, thanks! just left a few comments 🙂

…e < 24.9

Signed-off-by: Jon Koops <jonkoops@gmail.com>
@jonkoops
jonkoops force-pushed the fix/esm-transform-fallback-node-lt-24.9-v2 branch from 6f056f4 to bc8e8e7 Compare June 23, 2026 22:12
@jonkoops
jonkoops requested a review from SimenB June 23, 2026 22:14
@jonkoops

Copy link
Copy Markdown
Contributor Author

Code coverage failures seem unrelated to this PR, everything else is green, so I think this is good to go.

@SimenB SimenB left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks!

@SimenB
SimenB merged commit 1ac7378 into jestjs:main Jun 24, 2026
91 of 92 checks passed
@jonkoops

Copy link
Copy Markdown
Contributor Author

Thanks for landing this, @SimenB. Is there a release scheduled for Jest anytime soon?

@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 26, 2026
@SimenB

SimenB commented Aug 28, 2026

Copy link
Copy Markdown
Member

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

require-changelog If a PR does requires a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants