-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix: Support class instances in .toHaveProperty() matcher #5367
fix: Support class instances in .toHaveProperty() matcher #5367
Conversation
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.
A, nice!
Missing changelog entry, otherwise no comments (pending CI)
Codecov Report
@@ Coverage Diff @@
## master #5367 +/- ##
=======================================
Coverage 61.33% 61.33%
=======================================
Files 205 205
Lines 6924 6924
Branches 4 3 -1
=======================================
Hits 4247 4247
Misses 2676 2676
Partials 1 1 Continue to review full report at Codecov.
|
|
||
### Fixes | ||
|
||
* `[jest]` Add `import-local` to `jest` package. | ||
([#5353](https://github.com/facebook/jest/pull/5353)) | ||
* `[expect]` Support class instances in `.toHaveProperty()` matcher. |
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 also affected .toMatchObject()
as far as I can tell.
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.
in a good or bad way?
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.
In a very good way. It basically enables .toMatchObject()
to check if a given class implements an interface.
But in my opinion this should be included in the changelog regardless of whether one finds the change good or bad.
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.
It was more in the vein of "is this a regression we have to fix or not?". Good that it's not!
PR welcome to expand the changelog entry 🙂
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.
Should I include some tests just to make sure I identified that behavior correctly?
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.
That would be awesome!
- jestjs/jest#5367 added support for "interface" matching with getters
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
Checking for object own property with
Object.prototype.hasOwnProperty.call(object, value)
won't work on class instance getters. To support that, we can fall back to checkingobject.constructor.prototype
, because we knowobject
is an Object so we can actually get its class prototype which stores the getter.Also refactored the code a bit for perf (remove unnecessary
delete
) and readability (flattened if/else branching, use?w=1
to review)Resolves #5339
Test plan
Added a test or two.