Skip to content

Fix cross-realm Node global constructors#16205

Open
Peter1169 wants to merge 3 commits into
jestjs:mainfrom
Peter1169:peter/fix-node-global-instanceof
Open

Fix cross-realm Node global constructors#16205
Peter1169 wants to merge 3 commits into
jestjs:mainfrom
Peter1169:peter/fix-node-global-instanceof

Conversation

@Peter1169

@Peter1169 Peter1169 commented May 21, 2026

Copy link
Copy Markdown

Summary

Fixes #2549.

jest-environment-node already keeps the VM intrinsics for isolation, but Node APIs can still return values created in the parent realm. That makes checks like error instanceof Error, query.tag instanceof Array, and Object.getPrototypeOf(process.report.getReport()) === Object.prototype fail inside tests even though the values come from normal Node APIs.

This change teaches the test realm's native constructors to recognize matching parent-realm values through Symbol.hasInstance, while keeping the VM constructors in place. It also maps parent-realm native prototypes returned by Object.getPrototypeOf back to the corresponding test-realm prototypes.

The new e2e fixture covers:

  • arrays returned from Node's URL parser
  • errors thrown by fs
  • avoiding the old failure mode where a parent-realm Error matched unrelated test-realm subclasses
  • objects returned by Node's process report API

Test plan

  • corepack yarn@4.14.1 install --immutable
  • corepack yarn@4.14.1 build:js
  • corepack yarn@4.14.1 eslint --cache --fix packages/jest-environment-node/src/index.ts e2e/__tests__/instanceof.test.ts e2e/instanceof/__tests__/test.js
  • corepack yarn@4.14.1 build:ts
  • corepack yarn@4.14.1 jest e2e/__tests__/instanceof.test.ts --runInBand
  • npx -y node@18.20.8 .yarn/releases/yarn-4.14.1.cjs jest e2e/__tests__/instanceof.test.ts --runInBand
  • corepack yarn@4.14.1 jest e2e/__tests__/env.test.ts e2e/__tests__/json.test.ts e2e/__tests__/global.test.ts --runInBand
  • GLOBALS_CLEANUP=off|soft|on corepack yarn@4.14.1 jest --runInBand packages/jest-environment-node/src/__tests__
  • corepack yarn@4.14.1 check-changelog 16205
  • git diff --check

Also ran corepack yarn@4.14.1 check-copyright-headers; it currently fails on existing e2e/transform-linked-modules/ignored/symlink.js, which this PR does not touch.

@linux-foundation-easycla

linux-foundation-easycla Bot commented May 21, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@netlify

netlify Bot commented May 21, 2026

Copy link
Copy Markdown

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 6be6ccd
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/6a0f4f353c180d0008caaab1
😎 Deploy Preview https://deploy-preview-16205--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 May 21, 2026
@pkg-pr-new

pkg-pr-new Bot commented May 21, 2026

Copy link
Copy Markdown

Open in StackBlitz

babel-jest

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

babel-plugin-jest-hoist

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

babel-preset-jest

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

create-jest

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

@jest/diff-sequences

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

expect

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

@jest/expect-utils

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

jest

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

jest-changed-files

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

jest-circus

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

jest-cli

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

jest-config

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

@jest/console

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

@jest/core

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

@jest/create-cache-key-function

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

jest-diff

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

jest-docblock

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

jest-each

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

@jest/environment

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

jest-environment-jsdom

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

@jest/environment-jsdom-abstract

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

jest-environment-node

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

@jest/expect

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

@jest/fake-timers

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

@jest/get-type

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

@jest/globals

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

jest-haste-map

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

jest-jasmine2

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

jest-leak-detector

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

jest-matcher-utils

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

jest-message-util

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

jest-mock

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

@jest/pattern

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

jest-phabricator

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

jest-regex-util

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

@jest/reporters

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

jest-resolve

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

jest-resolve-dependencies

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

jest-runner

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

jest-runtime

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

@jest/schemas

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

jest-snapshot

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

@jest/snapshot-utils

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

@jest/source-map

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

@jest/test-result

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

@jest/test-sequencer

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

@jest/transform

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

@jest/types

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

jest-util

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

jest-validate

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

jest-watcher

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

jest-worker

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

pretty-format

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

commit: 6be6ccd

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

Labels

require-changelog If a PR does requires a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Jest globals differ from Node globals

1 participant