-
-
Notifications
You must be signed in to change notification settings - Fork 220
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
Add a toThrowWithMessage matcher #170
Conversation
Codecov Report
@@ Coverage Diff @@
## master #170 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 103 105 +2
Lines 505 536 +31
Branches 86 95 +9
=====================================
+ Hits 505 536 +31
Continue to review full report at Codecov.
|
4c213e6
to
ee823b3
Compare
README.md
Outdated
@@ -398,6 +399,30 @@ test('passes when value is a function', () => { | |||
}); | |||
``` | |||
|
|||
#### .toThrowWithMessage(type, message) | |||
|
|||
Use `.toThrowWithMessage` when checking if a callback function throws an error with a given error type and given error message. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it's worth mentioning that the message can be either a String
or RegExp
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds like a great idea! I've done the update.
if (!callback || typeof callback !== 'function') { | ||
return { | ||
pass: false, | ||
message: () => ` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spacing of these validation errors is tabbed in too much for the printouts (see the snapshots)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I've pushed in a change for this. Do take a look.
@natealcedo this is looking good just a few small comments :) |
…either be a `String` or a `RegExp`
Thanks @natealcedo 👍 |
What
This pull request adds in a new matcher for asserting that a callback function throws an error of a given type and given error message.
Why
This feature has been requested in core but after some discussion, it was decided that implementing it here would be the better solution.
This PR fixes the following issues
Notes
I've gone down the road of testing the matcher both by extending expect with this matcher and by invoking the matcher itself. Since this repository doesn't have some of the util tools that the Jest repo has, namely runJest, I am not able to test the failing scenarios since I can't capture whatever is logged to stdErr to assert that the error message is indeed correct. As such, I've just invoked the matcher itself and asserted whether it passes or not and used snapshot testing for asserting error messages.
Also, the goal for me in this matcher was to stick as close to the default
toThrowError
matcher. As such, this matcher also accepts both astring
andregex
for matching the error message.The code is abit verbose right now but I'm more than happy to have a discussion on how to amend it moving forward.
Housekeeping
yarn contributor
)