Should deepEqual use hasOwnProperty?
See original GitHub issueI found this out the hard way when a library was returning an instance of a constructor instead of a generic object. It looked the same via console.log, but the test was failing.
var Person = function(name) { this.name = name; }
Person.prototype.toString = function() { return 'My name is ' + this.name; }
var bob1 = new Person('bob')
var bob2 = { name: 'bob' }
expect(bob1).to.deep.equal(bob2) // false
The test output was pretty unhelpful:
0 passing (32ms)
1 failing
1) semver-utils should parse good ranges:
AssertionError: expected [ Array(1) ] to deeply equal [ Array(1) ]
+ expected - actual
at Context.<anonymous> (test/spec.js:14:12)
It seems like the default deepEqual
behavior should only check properties for which hasOwnProperty is true which is the behavior of node’s built-in assert.deepEqual. Or an alternative deepOwnEqual
with that behavior should be provided.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Object.prototype.hasOwnProperty() - JavaScript | MDN
The method returns false if the property is inherited, or has not been declared at all. Unlike the in operator, this method does...
Read more >When do I need to use hasOwnProperty()? - Stack Overflow
I read that we should always use hasOwnProperty when looping an object, because the object can be modified by something else to include...
Read more >What is Object.hasOwn() and why should we use it over ...
If the property is inherited, or does not exist, the method returns false. The hasOwnProperty() method also returns a boolean indicating whether the...
Read more >The Difference Between in and hasOwnProperty in JavaScript
The key difference is that in will return true for inherited properties, whereas hasOwnProperty() will return false for inherited properties.
Read more >Should.js API Documentation
To add new assertions, need to use Assertion.add method. Arguments. [propertyName] (string): Name of property to add. Default is 'should ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hmm, I’m coming back to this because expect just moved from node-deep-equal to is-equal.
I would say best create a own flag
to emulate other behaviors as a opt-in. With the appropriate documentation it should be ok.
There’s still the matter of which library will power the own flag. Do we edit https://github.com/chaijs/deep-eql or use something else ? Ultimately your choice probably ^^.
Thanks everyone for your input here.
We’ve added this to our Roadmap https://github.com/chaijs/chai/projects/2! We’ll be releasing chai 5 soon, but for now I’ll close this issue because it is tracked on our roadmap.