-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Confusing output from expect.toThrow #4724
Comments
Yeah, this is really annoying, and I would consider it a bug since it says what it expects to be matching but doesn't actually match against that. It appears to be only matching against Or, if worried about breaking changes, you could add a new expect such as, This is pretty frustrating since it's very useful in many cases to just check that the correct "type" of error is being thrown rather than the exact error message - just like |
Maybe the function signature |
Is there any workaround for this, or is it really not possible to check What's the holdup on getting this fixed? I can submit a pull request if the maintainers agree on what should be the correct, desired behaviour. I think @tamlyn original suggestion of matching against |
@pedrottimark are you fixing this? Seems like it would show the type error first, right, not just the stringified thing? |
Reducing confusion will be a part of the “Improve report when assertion fails” series of PR. @SimenB To improve the matcher criterion, what do you think about the following:
Concerning the proposed solution to implicitly include |
Update to previous comment, recommend object literal to match
parallel to error instance object, instead of as an optional second argument. EDIT: doesn’t support matching name as string and message as regexp. Is that criterion needed? |
For those interested, I implemented something similar actually in jest-extended. https://github.com/jest-community/jest-extended#tothrowwithmessagetype-message I have a pull request to handle the |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
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. |
Do you want to request a feature or report a bug?
A feature although you could argue the current behaviour is a bug.
What is the current behavior?
expect.toThrow
uses a differentexpected
string in its output that what it matches against internally.For example:
Produces this confusing error message:
In particular, this happens with errors from
joi
and it has tripped me up several times.What is the expected behavior?
Seems logical that toThrowMatchingStringOrRegexp should match against the same string that it displays as the expected output and that this should be
${error.name}: ${error.message}
.Changing the expected string would be a breaking change but breakages should be few and far between. You'd have to be explicitly expecting the name of the error to not appear in the error message.
As a bonus, being able to assert against the error name as a string instead of a constructor would be useful in cases where the error constructor is hidden away in a library.
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
The text was updated successfully, but these errors were encountered: