v8 deserialize() does not use same Object prototype #43564
Replies: 11 comments
-
I'm unable to reproduce. If I fix the example (by replacing |
Beta Was this translation helpful? Give feedback.
-
So this isn't quite as reproducible as I thought. Maybe it is Node.js version specific? I can 100% reproduce this in a Jest unit test with 14.19.1 on Microsoft Windows NT 10.0.19044.0 x64. |
Beta Was this translation helpful? Give feedback.
-
I was also able to reproduce this on a Jest test in CodeSandboxIo: https://codesandbox.io/s/relaxed-mestorf-e7yjy3?file=/src/test.js |
Beta Was this translation helpful? Give feedback.
-
Could |
Beta Was this translation helpful? Give feedback.
-
What I'm seeing is really confusing, but maybe I just don't fully understand the prototype chain here. In the same Jest test file, I created another test: it.only('Comparing versions of Object', () => {
const clone = deserialize(serialize(obj))
const prototype1 = Object.getPrototypeOf(obj)
const prototype2 = Object.getPrototypeOf(clone)
expect(prototype1.name).toEqual(prototype2.name)
expect(prototype1).toEqual(Object.prototype)
expect(prototype2).toEqual(Object.prototype)
expect(Object.prototype.isPrototypeOf(obj)).toBe(true)
expect(Object.prototype.isPrototypeOf(clone)).toBe(true)
}) Which yields:
So both the prototype of |
Beta Was this translation helpful? Give feedback.
-
Jest has own globals. This is a common pitfall. |
Beta Was this translation helpful? Give feedback.
-
So how on Earth can we create proper coverage of something that uses structured cloning? |
Beta Was this translation helpful? Give feedback.
-
Please reach out to jest. The only solution I know is to use a different test runner. |
Beta Was this translation helpful? Give feedback.
-
Ah, I see cracks in the curtain now. I changed
|
Beta Was this translation helpful? Give feedback.
-
Note that this still only fails on |
Beta Was this translation helpful? Give feedback.
-
Converted to a discussion since this is not a node bug. Refs jestjs/jest#2549. |
Beta Was this translation helpful? Give feedback.
-
Version
14.19.1
Platform
Microsoft Windows NT 10.0.19044.0 x64
Subsystem
v8
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
100% reproducible
What is the expected behavior?
When using the
v8.deserialize()
, one would think it theDefaultDeserializer
would maintain the sameObject
prototype as the original item as well as the older styleJSON.parse()
.What do you see instead?
Instead, the deserialized object has an
Object
prototype but it appears to be a different prototype with exactly the same contents. Even Jest gets confused and claims the prototypes "serialize to the same string" and yet test as not being the same via all calls.Additional information
No response
Beta Was this translation helpful? Give feedback.
All reactions