Skip to content

False positive behavior toStrictEqual #6759

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

Closed
sirian opened this issue Jul 25, 2018 · 11 comments
Closed

False positive behavior toStrictEqual #6759

sirian opened this issue Jul 25, 2018 · 11 comments

Comments

@sirian
Copy link

sirian commented Jul 25, 2018

🐛 Bug Report

.toStrictEqual doesn't perform === equality check for nested objects

To Reproduce

test("", () => {
    class Foo {}
    class Bar {}

    const p1 = new class A extends Foo{}();
    const p2 = new class A extends Bar{}();

    expect([p1]).toStrictEqual([p1]); // pass
    expect([p1]).toStrictEqual([p2]) // pass, but should not
});

Expected behavior

expect([p1]).toStrictEqual([p2]) should not pass

@SimenB
Copy link
Member

SimenB commented Jul 26, 2018

It's not ===, it's equality. UsetoBe for referential equality.

PR welcome if you think the docs can be improved :)

@SimenB SimenB closed this as completed Jul 26, 2018
@SimenB
Copy link
Member

SimenB commented Jul 26, 2018

@rickhanlonii I see you asked OP to open this. Why do you think it's a bug?

@sirian
Copy link
Author

sirian commented Jul 26, 2018

@SimenB more attention pls. I'm talking about array values equality rather than arrays itself.
Look at #6757 (comment)

@SimenB
Copy link
Member

SimenB commented Jul 26, 2018

Right. Still, your example is supposed to pass, it's not a bug. 2 instances of the same class are considered equal

@sirian
Copy link
Author

sirian commented Jul 26, 2018

@SimenB ok. But is there any way to perform deep/iterable toBe check?

For example to test memoizing.

Of course i could use loops like below or create custom matcher, but it will display bad errors.
Look

test("", () => {

    class HugeFoo {
        @memoize
        static huge(){}
    }

    function hugeTask() {
        return [10, HugeFoo.huge()]
    }

    const a1 = hugeTask();
    const a2 = hugeTask();
    expect(a1).toEqual(a2); 
    expect(a1).toStrictEqual(a2);

    expect(a1.length).toBe(a2.length);
    for (let i = 0; i < a1.length; i++) {
        expect(a1[i]).toBe(a2[i]);
    }
});

image

@SimenB
Copy link
Member

SimenB commented Jul 26, 2018

@mattphillips are there any matchers for this in jest-extended?

@rickhanlonii
Copy link
Member

2 instances of the same class are considered equal

Ah, my mistake

@sirian
Copy link
Author

sirian commented Jul 27, 2018

@rickhanlonii nevertheless bug exists when comparing 2 instances of different classes with identical name.
#6767

@sirian
Copy link
Author

sirian commented Jul 27, 2018

@rickhanlonii @SimenB
i think non-plain objects should be compared with === and not by structure. At least sentence 2 instances of the same class are considered equal is controversial new Boolean(true) and new Boolean(false) are instances of same class. so they should be considered equal?

@sirian
Copy link
Author

sirian commented Jul 27, 2018

@SimenB
Also toStrictEqual is ambiguous. From documentation

Use .toStrictEqual to test that objects have the same types as well as structure.

const object = Object(1);

expect(object).toStrictEqual(1); 
// pass, but should not - they have different types.
// typeof object !== typeof 1
expect(new Number(1)).toStrictEqual(new Number(2)); 
// fail, but they have same structure, and same type

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants