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.

Deep equality check for Errors

See original GitHub issue

Hi, because we use chai and chai-as-promised in our project, we recently tried to use .rejectedWith() in order to check for a custom error:

class HttpError extends Error {
    constructor(statusCode, message) {
        super(message);
        this.statusCode = statusCode;
        Object.setPrototypeOf(this, new.target.prototype);
        this.name = new.target.name;
    }
    toString() {
        return `${this.name}(${this.statusCode}): ${this.message}`;
    }
}

But the following check doesn’t work:

it('', async () => {
   const httpError = new HttpError(404, 'Not found.');
   await expect(Promise.reject(new HttpError(404, 'Not found.'))).to.be.rejectedWith(httpError);
});

This is - as @meeber kindly pointed out here - because of the strict === comparison of the throws() assertion in Chai itself, as chai-as-promised just emulates Chai’s throws().

I personally think that it’d be more useful for errors to have a deep equality check in place. Something like suggested in the linked issue: .to.deep.throw() => to.be.deep.rejectedWith() This deep equality check can’t be the same as the normal deep equality check for objects, because of the Error’s stack-trace property, which of course will be not the same in the most cases. I guess this approach would also not break the existing usage, so it might be a bit simpler than changing the error comparison as a whole.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
meebercommented, Oct 20, 2017

I agree with @janis91; I think our deep equality algorithm should special-case Error objects by ignoring the .stack property, but consider all other own and inherited enumerable properties.

Although the semantics are a bit weird, I also think Chai should support .deep.throw(errInstance) in order to perform a deep equality comparison against the thrown error, instead of strict equality. Such a change could ride in the wake of #1021.

4reactions
keithamuscommented, Jun 15, 2018

@MicahZoltu part of the plan for Chai 5 is to have matchers, so you could do something like:

expect(error1).to.deep.equal({
  a: 'hello',
  b: expect.to.be.an.error(Error, /goodbye/)
})

The exact syntax and mechanics are not yet hashed out, but it would likely be close to something like this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deeply equal function error in Javascript - Stack Overflow
I am trying to create a function to check whether two arrays are deeply equal to each other. An example would be: [1,...
Read more >
Deep Equality checking of Objects in Vanilla JavaScript ‍
i think you can use JSON.stringify the both objects then you can compare it ! the only issue here you need to catch...
Read more >
5 Different Ways to Deep Compare JavaScript Objects
Deep equality : Determines whether objects are equal by comparing each property in the operands. Referential equality can be determined with ...
Read more >
deep-equal - npm
Compare objects a and b , returning whether they are equal according to a recursive equality algorithm. If opts.strict is true , use...
Read more >
07 - Assert Deep Equality with .deepEqual - YouTube
This is how I think this works:1) Check the number of keys in both objects are the same2) Check that all keys in...
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