-
-
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
implement spyOnProperty method #5107
Conversation
ab6e1db
to
98acdff
Compare
packages/jest-runtime/src/index.js
Outdated
parent?: Module, | ||
paths?: Array<Path>, | ||
require?: (id: string) => any, | ||
exports: any, |
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.
please run eslint to fix all these whitespace changes
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.
i fixed every whitespace by hand.. i missed this file probably or it was overwritten later. i will fix it.
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.
Hmm, you shouldn't have to. Just yarn eslint . --fix
should be enough. I don't have issues with the lint integration, but I don't use VS code either
.vscode/settings.json
Outdated
@@ -9,5 +9,6 @@ | |||
"flow.useNPMPackagedFlow": true, | |||
"javascript.validate.enable": false, | |||
"jest.pathToJest": "yarn jest --", | |||
"prettier.eslintIntegration": true | |||
"prettier.eslintIntegration": true, | |||
"editor.formatOnSave": false |
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.
is this needed?
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.
I prefer not
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.
i can revert the dedicated commit if you prefer that way.
packages/jest-mock/src/index.js
Outdated
@@ -691,6 +691,65 @@ class ModuleMockerClass { | |||
return object[methodName]; | |||
} | |||
|
|||
spyOnProperty(object: any, propertyName: any, accessType = 'get'): any { | |||
if (typeof object !== 'object' && typeof object !== 'function') { |
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 should more closely match the normal spyOn
.
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.
actually this if
is the same of the spyOn
above. what do you mean? i have adapted the code to with with a property descriptor instead of a normal property. do i have missed something here?
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.
Stuff like "// IE 8 doesn't support definePropery
on non-DOM nodes" etc is not needed.
But anyways, can we make jest.spyOn
work, so this new function is unnecessary?
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.
keep in mind that making spyOn
work with getters/setters will introduce a breaking change between jasmine
and jasmine-light
shipped with jest
, adding a little extra step during migrations to jest
.
for this reason, i actually prefer following the original project and create a new method spyOnProperty
for that.
Any reason why we cannot make |
@SimenB i was thinking about it too. in the end i just followed the original jasmine PR to be more compliant with the original library. |
Making |
6c91646
to
9c751da
Compare
i've reverted the commit for |
38860c7
to
e9147a6
Compare
yesterday evening i tried to fix every errors in the tests.. one snapshots is still failing.
what's the next steps for this feature? at the moment i had to disable the unit tests in a firebase project due to the fact that i can't spy on getters with jest but i would like to restore the tests in these days.. |
#5132 should fix the failure you're seeing
We need tests showing that the new function works |
ok, i will rebase this branch when #5132 is merged. in the meanwhile i will try to implement some tests for this feature. |
e9147a6
to
65d884d
Compare
Codecov Report
@@ Coverage Diff @@
## master #5107 +/- ##
==========================================
- Coverage 60.7% 60.59% -0.12%
==========================================
Files 201 201
Lines 6691 6748 +57
Branches 4 3 -1
==========================================
+ Hits 4062 4089 +27
- Misses 2628 2658 +30
Partials 1 1
Continue to review full report at Codecov.
|
@SimenB i've implemented the tests and CI is green.. can you please review the code changes? thanks. |
Why can't we use |
@SimenB as i said above, to maintain consistency with jasmine apis. |
Parity with Jasmine's APIs is non-goal (we want to drop jasmine entirely, #4362). |
@SimenB are we sure that changing behaviour of btw a benefit that i see valuable of having the same semantics of also, besides the fact that we want to drop |
@SimenB any feedback? i need this PR shipped to restore unit tests involving |
packages/jest-jasmine2/src/index.js
Outdated
|
||
if (uncheckedCount) { | ||
uncheckedKeys = snapshotState.getUncheckedKeys(); |
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.
why was this changed? This seems unrelated.
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.
i got runtime errors when developing this feature when uncheckedCount
is falsy. do you think that this change is not required?
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.
I think this should be changed in a separate commit, with a test that is fixed by your change. Please revert it from this PR and send one for this issues specifically. Thank you.
this._spyOnProperty = function(obj, propertyName, accessType = 'get') { | ||
if (!obj) { | ||
throw new Error( | ||
'spyOn could not find an object to spy upon for ' + propertyName + '', |
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.
There is an empty string at the end. Can we end the sentence with a .
? Also, can we prefix the error with "Jest: "?
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.
i will do it.
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.
} | ||
|
||
if (!propertyName) { | ||
throw new Error('No property name supplied'); |
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.
Same as above
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.
i will fix it.
Can you format all the error messages properly? What if somebody wants to spy both on the getter and setter of a field, with separate spies? |
@cpojer good point. everything should work simply calling two times the what do you think about my reply? |
@phra yes please! |
`spyOn` in getters and setters is available starting from Jest v. 22.0.5, see jestjs#5107
`spyOn` in getters and setters is available starting from Jest v. 22.0.5, see #5107
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
In order to be able to spy on getters/setters we need to backport
spyOnProperty
method from Jasmine.implementspyOnProperty
methodimplement testsmerge top level invocation ofspyOn/spyOnProperty
Test plan