-
-
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
fix: spyOn should not rely on hasOwnProperty from the spied object #11721
Conversation
Hi @cexbrayat! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Codecov Report
@@ Coverage Diff @@
## master #11721 +/- ##
=======================================
Coverage 69.01% 69.01%
=======================================
Files 312 312
Lines 16335 16335
Branches 4734 4734
=======================================
Hits 11273 11273
Misses 5034 5034
Partials 28 28
Continue to review full report at Codecov.
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
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.
Test case demonstrates the problem well, and borrowing hasOwnProperty from the Object prototype is good practise in this kind of context. 👍 Especially since hasOwnProperty is not protected, meaning objects could even have their own implementation that produces unpredictable results.
Only thing I see missing is a changelog entry for the fix
e1f582a
to
a34e4b6
Compare
@sigveio Thanks for the feedback. |
This is definitely a good improvement |
@@ -1216,6 +1216,18 @@ describe('moduleMocker', () => { | |||
expect(originalCallArguments[1]).toBe(secondArg); | |||
expect(spy).not.toHaveBeenCalled(); | |||
}); | |||
|
|||
it('should not rely on hasOwnProperty', () => { |
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 description of the test I find not the best.
It still relies on hasOwnProperty as it is calling it from the Object.prototype
I would name it as should not throw an error if spying on object without prototype
or something like that
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 could be simplified to 'should work with object of null prototype'?
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.
sounds good
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.
Done 👍
The `spyOn` function uses `hasOwnProperty` from the spied object, but this method can be unavailable, for example if the object has the `null` prototype, or if it is a proxy that filters some keys. This arises in Vue 3 projects where the proxies returned by the framework do not expose all methods, and forces the testing library to manually patch the proxies with `hasOwnProperty` to let Jest do its work https://github.com/vuejs/vue-test-utils-next/blob/23d3d3e1f4178a87de5023f5255e0623653affdc/src/mount.ts#L493-L495 This commit changes the code to use `Object.prototype.hasOwnProperty` and fixes this issue.
a34e4b6
to
d614bb1
Compare
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.
thanks!
Updates jest and ts-jest to v27, as jest v27.1.0 includes a necessary fix to unblock further work on spying issues. See jestjs/jest#11721
vue-test-utils-next now uses jest v27.1+, which includes a fix for hasOwnPropery (see jestjs/jest#11721). This allows to remove the dirty workaround we had in our codebase, but users will have to update to Jest v27.1+.
vue-test-utils-next now uses jest v27.1+, which includes a fix for hasOwnPropery (see jestjs/jest#11721). This allows to remove the dirty workaround we had in our codebase, but users will have to update to Jest v27.1+.
Updates jest and ts-jest to v27, as jest v27.1.0 includes a necessary fix to unblock further work on spying issues. See jestjs/jest#11721
Updates jest and ts-jest to v27, as jest v27.1.0 includes a necessary fix to unblock further work on spying issues. See jestjs/jest#11721
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
The
spyOn
function useshasOwnProperty
from the spied object, but this method can be unavailable,for example if the object has the
null
prototype, or if it is a proxy that filters some keys.This arises in Vue 3 projects where the proxies returned by the framework do not expose all methods,
and forces the testing library to manually patch the proxies with
hasOwnProperty
to let Jest do its workhttps://github.com/vuejs/vue-test-utils-next/blob/23d3d3e1f4178a87de5023f5255e0623653affdc/src/mount.ts#L493-L495
Test plan
This commit changes the code to use
Object.prototype.hasOwnProperty
and fixes this issue.A unit test has been added to reproduce the issue.