question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should deepEqual use hasOwnProperty?

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Sinewykcommented, Dec 17, 2015

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

expect(A).to.deep.own.equal(B);

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 ^^.

0reactions
keithamuscommented, Jun 13, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found