expect.eql() returns true when deeply comparing different Error objects
See original GitHub issueI’ve run into an issue which I think is a bug when using deeply equals to compare two Error
objects. Even though the message of the errors is different the expectation still returns that they’re equal.
Here’s an example using Mocha with expect().to.eql
.
it('this test should fail', () => {
const expectedError = new Error('An error');
const error = new Error('A different error');
expect(error).to.eql(expectedError);
});
Mocha passes this test even though I’d expect this to fail as the two errors have different message strings.
Is this a bug? I couldn’t find any similar problems when searching the issues.
Thanks, Marc
Issue Analytics
- State:
- Created 8 years ago
- Comments:18 (12 by maintainers)
Top Results From Across the Web
.expect.eql() | TestController | Test API | API | Docs - TestCafe
Asserts that actual is equal to expected . A comparison value. See actual parameter value. An expected value.
Read more >Chai deep.equal failing on comparison of {} to promise instance
Error objects are compared by reference (see chaijs/chai/issues/608):. eql(new ... var err = new Error('msg'); eql(err, err).should.be.true;.
Read more >Expect / Should - Chai Assertion Library
ok. Asserts that the target is a truthy value (considered true in boolean context). However, it's often best to assert that the target...
Read more >5 Different Ways to Deep Compare JavaScript Objects
Using the isEqual() method from this library, we can perform a deep comparison between the given operands. It will return a Boolean value...
Read more >How to Compare Object Instances in your Unit Tests Quickly ...
It might not be feasible to manually compare EVERY field with expected values in another object. Here's xUnit's Assert.Equal<T>(T expected, T actual) method ......
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
Just FYI - this is already fixed in an outstanding PR on deep-eql; see https://github.com/chaijs/deep-eql/pull/14/files#diff-910eb6f57886ca16c136101fb1699231R268
Hey @MarcL thanks for the issue.
This definitely looks like a bug - we want your assertion to fail. The reason why it’s not is a little… nuanced though:
This kind of leaves us in a tricky position, because we could solve this with several different solutions, but each one has it’s own pitfalls:
Object.getOwnPropertyNames
, but this would be a breaking change for lots of people - potentially causing more issues. This also may cause issues becausestack
is a enumerable property and trying to do equality on this will always fail because the line/column numbers will be different for every error. I am sure we had a discussion about enumerability and deep equality somewhere but I can’t find the issue now.Error
s and specifically check for message - but this could be a slippery slope - how many types do we treat specially? How do we document to users that these behaviours are happening?expect(error).to.be.same.error(otherError)
but of course this wont solve the fact thateql
wont work and so its not really fixing your issue.I’m not sure what the right solution is to this. As such - I’ve labeled it as
more-discussion-needed