-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: structuredClone
fails on toStrictEqual
#14011
Comments
structuredclone
fails on toStrictEqual
structuredClone
fails on toStrictEqual
I was playing with import assert from "node:assert";
import { expect } from "@jest/globals";
// equality
assert.deepEqual(JSON.parse(JSON.stringify(value)), value); // pass
expect(JSON.parse(JSON.stringify(value))).toEqual(value); // pass
assert.deepEqual(structuredClone(value), value); // pass
expect(structuredClone(value)).toEqual(value); // pass
// strict equality
assert.deepStrictEqual(JSON.parse(JSON.stringify(value)), value); // pass
expect(JSON.parse(JSON.stringify(value))).toStrictEqual(value); // pass
assert.deepStrictEqual(structuredClone(value), value); // fail !!!
expect(structuredClone(value)).toStrictEqual(value); // fail !!! Hm.. if Node and Expect agrees, that is correct behaviour. Or did I miss something? |
Might be this is the explanation: Object.getPrototypeOf(value) === Object.getPrototypeOf(JSON.parse(JSON.stringify(value))) // true
Object.getPrototypeOf(value) === Object.getPrototypeOf(structuredClone(value)) // false |
Okay, it looks like this might be a limitation of
|
My gut feeling is that I would not want to stop using
I wish to not loose all these checks when using I am not sure of the solution. One option could be to in a major version (i.e. a breaking change) omitting Any other options we have? |
Indeed it would be useful to be able to use |
@mrazauskas that is one solution. It kind of goes against other APIs at https://jestjs.io/docs/expect. Few function have a second argument. And if they do, it is not an object (possibly for readability?). If a second argument is the way to go I think this is nicer: I am not sure if @SimenB has more input on how a new API to cover this should look. Argument, new function or something else. |
Not sure how you define readability. A bag of options is easier to extend in the future and is understandable without looking at the docs:
|
Yeah, I agree it is subjective. Would a new |
Great idea! I could see I guess prototype should be somehow replaced for both objects before sending them into |
Selfish wish would be for it to be part of the core Jest expect functions, since we avoid Jest Extended as to not complicate users with too many options. But that is entirely subjective, as much is 😆 One reason to keep it on the core Jest Expect lib would be that As for the question about |
By the way, I was trying to implement a custom matcher and came to this idea: const value = { test: 123 };
const received = structuredClone(value);
expect(received).toStrictEqual(structuredClone(value)); // pass !!! |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
I suppose this is still a great feature to have. |
Hi there ! I just stumbled upon the same issue, and I agree that, given One particular sample that should pass IMO is the following: describe('foo', () => {
test('date check', () => {
const date = new Date();
expect(structuredClone(date)).toStrictEqual(date);
});
}); This currently fails with
This is key to me because using the |
@Marchelune if you read this thread and #14011 (comment) and also the comment after it, it answers the question. |
@thernstig I did read that and I saw the prototypes aren't the same but it still doesn't explain why it is so. |
@thernstig it specifically says |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Unstale |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Feels important enough to keep alive as I suspect modern JavaScript will use structuredClone |
Not stale |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Not stale |
Not sure if it is the same problem, but it also fails in the following test: it('should verify the constructor type', () => {
const x = new Date()
const y = structuredClone(x)
expect(x).toEqual(expect.any(Date)) // ✅ pass
expect(y).toEqual(expect.any(Date)) // ❎ fails
}) |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
still a problem IMO |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
still not resolved |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Still unresolved |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Not stale |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Still an issue in 29.7 |
Version
29.4.0
Steps to reproduce
npm install
npm test
Expected behavior
I expect to see objects cloned with
structuredClone
to passtoStrictEqual
Actual behavior
The values do not pass
toStrictEqual
(but do passtoEqual
).However
JSON.parse(JSON.stringify())
does passtoStrictEqual
Additional context
The test output can be seen in this GitHub Actions run.
Environment
The text was updated successfully, but these errors were encountered: