-
-
Notifications
You must be signed in to change notification settings - Fork 698
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
Deep equality check for Errors #1065
Comments
Hey @janis91 thanks for the issue. I'm inclined to agree. But actually right now the deep-eql algorithm doesn't do deep equality on errors, as you mention the I would say it might be an easier point to make that I have no firm opinion any way on this, just presenting the current state of mind here. |
Hi @keithamus thanks for sharing your thoughts. I basically like your suggestion. But I would go even further: Beside that we have to distinguish if it is an I hope this goes in a right way. |
While it is possible to throw any expression we should really only encourage the throwing of Error objects, so I'm not so interested in supporting generic objects. Let's get more eyes on this problem before moving toward a particular solution. I guess @meeber might have some feedback, given chaijs/chai-as-promised#226 (comment) |
I agree with @janis91; I think our deep equality algorithm should special-case Although the semantics are a bit weird, I also think Chai should support |
@meeber @keithamus Is there any progress on this, so far? I could try to help if you let me know where I should start. I think a |
@janis91 Things are slow-going at the moment, but this is still on the radar, including a semi-related issue in #907. PR #1021 is still alive, waiting on a follow-up review of chaijs/check-error#18. |
We've got a lot of PRs and issues open around this. It's definitely something on the radar, and we're trying to close a lot of issues so we can stay focussed. Let's close this so we can keep focussed. |
I just upgraded Chai and a test that was previously passing is now failing because the following is true: expect(new Error('apple')).to.not.deep.equal(new Error('apple')); Reading through issue backlog, it appears that Error comparisons used to only do a type check during deep compare, but were changed to do full comparisons. Unfortunately this puts me in an awkward situation because in my real scenario the error is a property on a nested object, so I don't see any way to compare other than to manually compare every property of the entire object tree except for the error. Are there any workarounds for those of us that were broken by the behavior change? Is a rollback the most reasonable solution? |
@MicahZoltu we'll soon have an |
@keithamus Will that be able to be used as part of a const error1 = [{a: 'hello', b: new Error('goodbye')}, ...]
const error2 = [{a: 'hello', b: new Error('goodbye')}, ...]
expect(error1).to.deep.equal(error2) |
@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. |
@keithamus That'd be really nice. I think this is quite a big step forward. I like this approach. |
Actually the import * as assert from 'assert'
assert.deepStrictEqual(
new Error('A'),
new Error('A')
) // passes |
- Use better error messages with more context. - Unify their validation logic and share tests. - Validate also type of the name. - Refactor node (Script/Category) parser tests for easier future changes and cleaner test code (using `TestBuilder` to do dirty work in unified way). - Add more tests. Custom `Error` properties are compared manually due to `chai` not supporting deep equality checks (chaijs/chai#1065, chaijs/chai#1405).
Wondering what is happening with Chai 5? |
Not a lot. Lack of time and developer burnout mean there has been little progress on Chai. Recruiting for maintainers is challenging, and retaining maintainers is even harder. It's a struggle to make changes to |
Understood and thank you for sharing this with me. I don't have a lot of time, but I'm also not saying no, just working on a startup which is taking about 200% of my time ;-) One incentive to help attract maintainers is that github copilot comes free for maintainers of large projects: https://copilot.github.com I've been using it 20 days....and I'm def going to continue with it. |
- Use better error messages with more context. - Unify their validation logic and share tests. - Validate also type of the name. - Refactor node (Script/Category) parser tests for easier future changes and cleaner test code (using `TestBuilder` to do dirty work in unified way). - Add more tests. Custom `Error` properties are compared manually due to `chai` not supporting deep equality checks (chaijs/chai#1065, chaijs/chai#1405).
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:But the following check doesn't work:
This is - as @meeber kindly pointed out here - because of the strict
===
comparison of thethrows()
assertion in Chai itself, as chai-as-promised just emulates Chai'sthrows()
.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.
The text was updated successfully, but these errors were encountered: