Skip to content
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

How to compare Object.create()? #620

Closed
fundon opened this issue Jan 27, 2015 · 7 comments
Closed

How to compare Object.create()? #620

fundon opened this issue Jan 27, 2015 · 7 comments
Labels
assert Issues and PRs related to the assert subsystem.

Comments

@fundon
Copy link
Contributor

fundon commented Jan 27, 2015

Using Object.create method to create object.
How to compare them for equality or deep equality?

Use Object.getPrototypeOf?

e.g.

var a = Object.create(null);
var b = Object.create({});
assert.deepEqual(a, b);

var a = Object.create({});
var b = Object.create({a:233});
assert.deepEqual(a, b);
@vkurchatkin vkurchatkin added the assert Issues and PRs related to the assert subsystem. label Jan 27, 2015
@vkurchatkin
Copy link
Contributor

see nodejs/node-v0.x-archive#4523
it's a bug in CommonJS: http://wiki.commonjs.org/wiki/Unit_Testing/1.0

7.4. For all other Object pairs, including Array objects, equivalence is determined by having the same number of owned properties (as verified with Object.prototype.hasOwnProperty.call), the same set of keys (although not necessarily the same order), equivalent values for every corresponding key, and an identical "prototype" property.

obviously, it had to be __proto__, [[Prototype]] or something like this
changing this behaviour breaks 3 tests

/cc @iojs/owners

@bnoordhuis
Copy link
Member

I'm alright with fixing it. assert.deepEqual({}, []) doesn't throw. That's clearly a bug.

@fundon
Copy link
Contributor Author

fundon commented Jan 27, 2015

@vkurchatkin thanks, great work.

vkurchatkin added a commit to vkurchatkin/node that referenced this issue Jan 27, 2015
Objects with different prototypes should not be considered deep equal,
e.g. `assert.deepEqual({}, [])` should throw. Previously `deepEqual`
was implemented as per CommonJS Unit Testing/1.0 spec. It states that
objects should have 'an identical "prototype" property' to be
considered deep equal, which is incorrect. Instead object prototypes
should be compared, i.e. `Object.getPrototypeOf` or `__proto__`.

Fixes: nodejs#620
vkurchatkin added a commit that referenced this issue Feb 9, 2015
`deepStrictEqual` works the same way as `strictEqual`, but
uses `===` to compare primitives and requires prototypes of
equal objects to be the same object.

Fixes: nodejs/node-v0.x-archive#7161
Fixes: #620
PR-URL: #639
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-by: Rod Vagg <rod@vagg.org>
@vkurchatkin
Copy link
Contributor

you can use deepStrictEqual introduced in 3f473ef, which compares prototypes using ===

@fundon
Copy link
Contributor Author

fundon commented Feb 9, 2015

Thanks, great.

@jokeyrhyme
Copy link

I just noticed that this does NOT throw:

assert.deepEqual(Object.create({tea:'chai'}), Object.create({tea:'black'}))

Weird.

@vkurchatkin
Copy link
Contributor

@jokeyrhyme use deepStrictEqual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert Issues and PRs related to the assert subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants