Skip to content

fix(expect-utils): handle cross-realm constructors in toStrictEqual#15959

Open
JamieMagee wants to merge 1 commit intojestjs:mainfrom
JamieMagee:fix/structuredclone-tostrictequal
Open

fix(expect-utils): handle cross-realm constructors in toStrictEqual#15959
JamieMagee wants to merge 1 commit intojestjs:mainfrom
JamieMagee:fix/structuredclone-tostrictequal

Conversation

@JamieMagee
Copy link

@JamieMagee JamieMagee commented Feb 15, 2026

Summary

structuredClone runs in the outer Node.js realm, so its return values have different constructor references than objects created inside Jest's VM context. This made toStrictEqual fail with "serializes to the same string" for every built-in type (plain objects, Maps, Sets, Dates, RegExps).

When constructors don't match by identity, we now check if both are native built-ins with the same name. User-defined classes still require identity equality.

Changed typeEquality and iterableEquality in expect-utils/src/utils.ts.

Fixes #14011

Test plan

10 unit tests added using vm.createContext() to create cross-realm objects. Covers all affected built-in types, plus a negative case for user-defined classes with the same name.

$ yarn jest packages/expect/ packages/expect-utils/
Test Suites: 17 passed, 17 total
Tests:       1095 passed, 1095 total

@netlify
Copy link

netlify bot commented Feb 15, 2026

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit ecb0380
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/69b998b4b768fe0008dc4fdb
😎 Deploy Preview https://deploy-preview-15959--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.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Feb 15, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: JamieMagee / name: Jamie Magee (ecb0380)

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 15, 2026

Open in StackBlitz

babel-jest

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

babel-plugin-jest-hoist

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

babel-preset-jest

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

create-jest

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

@jest/diff-sequences

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

expect

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

@jest/expect-utils

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

jest

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

jest-changed-files

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

jest-circus

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

jest-cli

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

jest-config

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

@jest/console

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

@jest/core

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

@jest/create-cache-key-function

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

jest-diff

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

jest-docblock

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

jest-each

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

@jest/environment

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

jest-environment-jsdom

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

@jest/environment-jsdom-abstract

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

jest-environment-node

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

@jest/expect

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

@jest/fake-timers

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

@jest/get-type

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

@jest/globals

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

jest-haste-map

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

jest-jasmine2

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

jest-leak-detector

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

jest-matcher-utils

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

jest-message-util

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

jest-mock

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

@jest/pattern

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

jest-phabricator

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

jest-regex-util

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

@jest/reporters

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

jest-resolve

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

jest-resolve-dependencies

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

jest-runner

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

jest-runtime

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

@jest/schemas

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

jest-snapshot

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

@jest/snapshot-utils

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

@jest/source-map

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

@jest/test-result

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

@jest/test-sequencer

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

@jest/transform

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

@jest/types

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

jest-util

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

jest-validate

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

jest-watcher

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

jest-worker

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

pretty-format

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

commit: ecb0380

structuredClone returns objects whose constructors come from the outer
VM context. typeEquality and iterableEquality compared constructors by
identity (===), which always failed across realms — even for plain
objects, Maps, Sets, Dates, and RegExps.

When constructors don't match by identity, fall back to comparing
constructor names, but only when both are native built-in functions.
User-defined classes still require identity equality, so toStrictEqual
keeps distinguishing e.g. two different classes named Child.

Fixes jestjs#14011
@JamieMagee JamieMagee force-pushed the fix/structuredclone-tostrictequal branch from 2870469 to ecb0380 Compare March 17, 2026 18:08
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.

[Bug]: structuredClone fails on toStrictEqual

1 participant