Skip to content

Ambigious comparing primitive with object #6784

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 31, 2018 · 5 comments
Closed

Ambigious comparing primitive with object #6784

sirian opened this issue Jul 31, 2018 · 5 comments

Comments

@sirian
Copy link

sirian commented Jul 31, 2018

🐛 Bug Report

Primitive values compare with objects is ambigious

const Bool = Boolean;
const classes = [
    Boolean,
    class Boolean extends Bool {},
    class Boolean2 extends Bool {},
    class Boolean {
        static [Symbol.hasInstance](ctor) {return Boolean === ctor;}
        valueOf() {return false;}
        [Symbol.toPrimitive]() {return false;}
    },
];

test.each(classes)("%O", (ctor) => {
    const expected = new ctor(false);
    expected.x = 1;

    expect(false).toEqual(expected);
    expect(false).toStrictEqual(expected);
});

Look at example. there are 4 test cases

  1. Boolean - native Boolean constructor
  2. Boolean extending native Boolean
  3. Boolean2 extending native Boolean
  4. fake class Boolean

At all iterations true compared with some object (more over - I explicitly set expected.x = 1)

Results

✓ Boolean
✓ [class Boolean extends Bool]
✕ [class Boolean2 extends Bool] fails on toStrictEqual - Comparing two different types of values. Expected object but received boolean.
✕ [fake class Boolean] fails on toEqual - Comparing two different types of values. Expected object but received boolean.

Expected behavior

All of these test should fall because true not equal object with x = 1 (at least toStrictEqual)

@sirian
Copy link
Author

sirian commented Jul 31, 2018

Another great example:

test("", () => {
    const obj = new Boolean(false);
    const proxy = new Proxy(obj, {});

    expect(false).toEqual(obj); // pass 
    expect(false).toStrictEqual(obj); // pass
    expect(false).toEqual(proxy); // fail
    expect(false).toStrictEqual(proxy); // fail
});

@SimenB
Copy link
Member

SimenB commented Sep 17, 2018

expect(false).toEqual(new Boolean(false)); makes sense, I think, false === Boolean(false) // true

not sure if this is a bug or expected behaviour? @thymikee @rickhanlonii thoughts?

@thymikee
Copy link
Collaborator

Also not sure, I'd need to check the spec to see if all this makes sense 🤷‍♂️

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

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

No branches or pull requests

3 participants