-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Testing promise rejection error code #4532
Comments
Reference: this was a small workaround for 20- as seen in here: #3601 Now there is no workaround anymore. |
I recommend changing your assertions to |
Those are no plain Errors and I do not have access to the original constructor in the client code, so The error instances are generated by sub-libraries that do not expose the constructor. The only assurance is that the error code is that one, and that is the reason for me to test it by one property. Any recommendation in that situation? I am pretty sure this should be fairly common. PS, haven't tested it yet but wouldn't the stack trace be different, and so the |
This works: test('check code property', () => {
const err = new Error();
err.code = 404;
return expect(Promise.reject(err)).rejects.toHaveProperty('code', 404);
}); |
@SimenB this example not works:
|
It works: https://repl.it/@SimenB/ComplexGainsboroGnudebugger You either have a typo or you're on a really old version of Jest |
Ops! my global jest version is 19, now updated and works fine! Thanks :) |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
First, I love jest and you guys have made a great work here. In Jest 21.0
.toMatchObject()
changed the way it handles Errors. I used to do this in Jest 20-:But now I have to pass the same full
Error
and cannot pass just a part of it. Some times errors are easy and can be built in the spot, but some times they might have more properties. An alternative would be to be able to access the object (error) returned byrejects
but as noted here the resolve/reject value cannot be accessed like this with Jest.This was really useful for testing just the error code, is there an equivalent way of doing this in Jest 21 without writing too much boilerplate? My suggestion: to add a matcher for errors. Something like this:
.rejects.withErrorCode()
Or even more generic like this:
.rejects.withError({ code: 'ERROR_CODE' })
Or just leave the specific case when there is a plain object being passed to compare by properties instead of by the full thing:
.rejects.toMatchObject({ code: 'ERROR_CODE' })
Any of these situations would make IMHO testing for rejection errors much easier. I know that I can write my own extension, but I think testing for rejection error codes should be fairly commonplace. If I am missing something and there is an easy way of testing for rejection codes please let me know.
The text was updated successfully, but these errors were encountered: