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
The "cause" property of Error is ignored when matching #5697
Comments
This is documented behavior: https://vitest.dev/api/expect.html#toequal
|
Hi, @sheremet-va. Got it. Can you give me an example of using |
I don't believe Perhaps something like this? const error = vi.throws(fn)
expect(error).toEqual({ message: '', cause: '' }) |
@sheremet-va, also, any chance to revisit this?
I assume |
Technically you can use chai API for this: expect(() => {
throw new Error('test', { cause: new Error('cause') });
})
.throws()
.property('cause')
.eql(new Error('cause')) I am not sure what the best course of action is here, I also don't really like that it only checks the message. Will put it on the board to discuss with the team. If anyone has any ideas here, feel free to suggest here. Maybe adding |
I think there are a few possible directions to this:
That's a good call! I will remember this for myself but it won't work for what I'm preparing for my students right now. Not checking arbitrary properties is okay. But expect(fn).toThrow(new Error('message', { cause: 123 }))
Alternatively, if this worked it'd also be great: // Return me the error thrown by "fn"
// or throw if "fn" doesn't throw.
// Returning null is also fine, depends
// if we want to treat this as implicit assertion.
const error = vi.throws(fn)
expect(error.message).toBe('message')
expect(error.cause).toBe('cause') |
I see your point. It would be nice to check Not sure about |
Only if explicitly specified in the assertion 👍 const error = new Error('message', {
cause: new Error('another', {
cause: 123
})
})
expect(error).toEqual(new Error('message')) // OK!
expect(error).toEqual(new Error('message', { cause: new Error('another') })) // OK!
expect(error).toEqual(new Error('message', { cause: new Error('another', { cause: 123 }) })) // OK!
expect(error).toEqual(new Error('message', { cause: new Error('another', { cause: 'bad' }) })) // X
expect(error).toStrictEqual(new Error('message')) // X
expect(error).toStrictEqual(new Error('message', { cause: new Error('another') })) // X
expect(error).toStrictEqual(new Error('message', { cause: new Error('another', { cause: 'bad' }) })) // X
expect(error).toStrictEqual(new Error('message', { cause: new Error('another', { cause: 123 }) })) // OK! |
Something similar is brought up here Comparing only It turns out Node doesn't check import assert from "node:assert";
// ok
assert.deepStrictEqual(
new Error('x', { cause: 'foo' }),
new Error('x', { cause: 'bar' })
); This is not stopping Vitest from checking |
Describe the bug
Vitest ignores the
cause
property whenever performing any equality onError
instances.Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-fq9xyu?file=test%2Fbasic.test.ts&initialPath=__vitest__/
System Info
Irrelevant. The issue is in the matcher.
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: