Skip to content
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

Merged
merged 4 commits into from
Aug 30, 2018
Merged

[expect] Fix TypeError in toBeInstanceOf on null or undefined #6912

merged 4 commits into from
Aug 30, 2018

Conversation

dhoulb
Copy link
Contributor

@dhoulb dhoulb commented Aug 29, 2018

Summary

When using toBeInstanceOf() on an undefined or null value Jest attempts to use undefined.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() for undefined and null and confirmed reasonable error messages are now returned for these values.

@@ -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
Copy link
Collaborator

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-io
Copy link

Codecov Report

Merging #6912 into master will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
packages/expect/src/matchers.js 97.45% <ø> (ø) ⬆️
packages/babel-jest/src/index.js 91.22% <0%> (+2.51%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8e72341...dce77f6. Read the comment docs.

@SimenB
Copy link
Member

SimenB commented Aug 30, 2018

Pleas update the changelog as well 🙂

@dhoulb
Copy link
Contributor Author

dhoulb commented Aug 30, 2018

@SimenB Ah, thanks for the reminder!

dhoulb and others added 4 commits August 30, 2018 15:03
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"
@SimenB SimenB merged commit bfba21c into jestjs:master Aug 30, 2018
rubennorte added a commit that referenced this pull request Aug 30, 2018
@dhoulb dhoulb deleted the bugfix-instance-of-non-object branch September 10, 2018 16:21
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants