fix(jest-runtime): fall back to native ESM when a .js file contains ESM syntax#16152
Merged
Conversation
…SM syntax but lacks a type:module marker
✅ Deploy Preview for jestjs ready!
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
babel-jest
babel-plugin-jest-hoist
babel-preset-jest
create-jest
@jest/diff-sequences
expect
@jest/expect-utils
jest
jest-changed-files
jest-circus
jest-cli
jest-config
@jest/console
@jest/core
@jest/create-cache-key-function
jest-diff
jest-docblock
jest-each
@jest/environment
jest-environment-jsdom
@jest/environment-jsdom-abstract
jest-environment-node
@jest/expect
@jest/fake-timers
@jest/get-type
@jest/globals
jest-haste-map
jest-jasmine2
jest-leak-detector
jest-matcher-utils
jest-message-util
jest-mock
@jest/pattern
jest-phabricator
jest-regex-util
@jest/reporters
jest-resolve
jest-resolve-dependencies
jest-runner
jest-runtime
@jest/schemas
jest-snapshot
@jest/snapshot-utils
@jest/source-map
@jest/test-result
@jest/test-sequencer
@jest/transform
@jest/types
jest-util
jest-validate
jest-watcher
jest-worker
pretty-format
commit: |
There was a problem hiding this comment.
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
CjsParseErrorand useses-module-lexerto detect “module syntax” on CJS parse/compile failures, enabling a native-ESM retry. - Updates the CJS-as-ESM synthetic-module path to return
nullonCjsParseError, allowing callers to fall back toloadEsmModule/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. |
…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]>
| : esmError; | ||
| }); | ||
| if (!(error instanceof CjsParseError)) throw error; | ||
| return this.loadEsmModule(modulePath); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
For #16144
Test plan
Tests added