Repository navigation
fix(jest-runtime): fall back to native ESM when a .js file contains ESM syntax - #16152
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. |
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 <noreply@anthropic.com>
| : esmError; | ||
| }); | ||
| if (!(error instanceof CjsParseError)) throw error; | ||
| return this.loadEsmModule(modulePath); |
|
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. |
Summary
For #16144
Test plan
Tests added