-
-
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
[expect]
Fix TypeError in toBeInstanceOf
on null
or undefined
#6912
Conversation
packages/expect/src/matchers.js
Outdated
@@ -183,7 +183,9 @@ const matchers: MatchersObject = { | |||
constructor.name || String(constructor), | |||
)}\n` + | |||
`Received constructor: ${RECEIVED_COLOR( | |||
received.constructor && received.constructor.name, | |||
received !== undefined && received !== null |
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.
received != null
will do both the checks
Codecov Report
@@ Coverage Diff @@
## master #6912 +/- ##
==========================================
+ Coverage 66.98% 66.98% +<.01%
==========================================
Files 250 250
Lines 10365 10360 -5
Branches 4 3 -1
==========================================
- Hits 6943 6940 -3
+ Misses 3421 3419 -2
Partials 1 1
Continue to review full report at Codecov.
|
Pleas update the changelog as well 🙂 |
@SimenB Ah, thanks for the reminder! |
If using toBeInstanceOf() on an undefined or null value Jest attempts to read `undefined.constructor` which generates an unhelpful error along the lines of "TypeError: Cannot read property 'constructor' of undefined". This fixes the bug so that it's more robust and returns the normal matcher error like "Expected X, received X"
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
When using
toBeInstanceOf()
on anundefined
ornull
value Jest attempts to useundefined.constructor
in its error message, which throws a TypeError along the lines of "TypeError: Cannot read property 'constructor' of undefined"This makes the matcher more robust and returns a normal matcher error along the lines of "Expected X, received X"
Test plan
Also added two snapshot tests for
toBeInstanceOf()
forundefined
andnull
and confirmed reasonable error messages are now returned for these values.