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

implement spyOnProperty method #5107

Merged
merged 18 commits into from
Jan 8, 2018
Merged

Conversation

phra
Copy link
Contributor

@phra phra commented Dec 18, 2017

Summary

In order to be able to spy on getters/setters we need to backport spyOnProperty method from Jasmine.

  1. implement spyOnProperty method
  2. implement tests
  3. merge top level invocation of spyOn/spyOnProperty

Test plan

  1. tests are implemented.

parent?: Module,
paths?: Array<Path>,
require?: (id: string) => any,
exports: any,
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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

@SimenB SimenB requested a review from aaronabramov December 18, 2017 13:48
@@ -9,5 +9,6 @@
"flow.useNPMPackagedFlow": true,
"javascript.validate.enable": false,
"jest.pathToJest": "yarn jest --",
"prettier.eslintIntegration": true
"prettier.eslintIntegration": true,
"editor.formatOnSave": false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer not

Copy link
Contributor Author

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.

@@ -691,6 +691,65 @@ class ModuleMockerClass {
return object[methodName];
}

spyOnProperty(object: any, propertyName: any, accessType = 'get'): any {
if (typeof object !== 'object' && typeof object !== 'function') {
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Contributor Author

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.

@SimenB
Copy link
Member

SimenB commented Dec 18, 2017

Any reason why we cannot make spyOn work on getters?

@phra
Copy link
Contributor Author

phra commented Dec 18, 2017

@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.

@cpojer
Copy link
Member

cpojer commented Dec 18, 2017

Making spyOn work itself would be awesome.

@phra phra force-pushed the feat/spy-on-property branch 2 times, most recently from 6c91646 to 9c751da Compare December 18, 2017 14:17
@phra
Copy link
Contributor Author

phra commented Dec 18, 2017

i've reverted the commit for editor.formatOnSave and fixed the indentation.

@phra phra force-pushed the feat/spy-on-property branch 2 times, most recently from 38860c7 to e9147a6 Compare December 18, 2017 20:58
@phra
Copy link
Contributor Author

phra commented Dec 19, 2017

yesterday evening i tried to fix every errors in the tests.. one snapshots is still failing.

Summary of all failing tests
 FAIL  integration_tests/__tests__/jest.config.js.test.js (10.23s)
  ● traverses directory tree up until it finds jest.config

    expect(value).toMatchSnapshot()
    
    Received value does not match stored snapshot 2.
    
    - Snapshot
    + Received
    
    - "PASS ../../../__tests__/a-banana.js
    + "PASS ../../../__tests__/a-banana.js (6.798s)
        ✓ banana
        ✓ abc
      
      "

      60 |   const {rest, summary} = extractSummary(stderr);
      61 |   expect(status).toBe(0);
    > 62 |   expect(rest).toMatchSnapshot();
      63 |   expect(summary).toMatchSnapshot();
      64 | });
      65 | 
      
      at Object.<anonymous>.test (integration_tests/__tests__/jest.config.js.test.js:62:16)

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..

@SimenB
Copy link
Member

SimenB commented Dec 19, 2017

#5132 should fix the failure you're seeing

what's the next steps for this feature?

We need tests showing that the new function works

@phra
Copy link
Contributor Author

phra commented Dec 19, 2017

ok, i will rebase this branch when #5132 is merged. in the meanwhile i will try to implement some tests for this feature.

@phra phra force-pushed the feat/spy-on-property branch from e9147a6 to 65d884d Compare December 19, 2017 12:54
@codecov-io
Copy link

codecov-io commented Dec 19, 2017

Codecov Report

Merging #5107 into master will decrease coverage by 0.11%.
The diff coverage is 48.21%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
...ackages/jest-jasmine2/src/jasmine/jasmine_light.js 0% <0%> (ø) ⬆️
packages/jest-jasmine2/src/jasmine/spy_registry.js 0% <0%> (ø) ⬆️
packages/jest-mock/src/index.js 86.18% <100%> (+1.34%) ⬆️

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 e74fbff...224eb25. Read the comment docs.

@phra
Copy link
Contributor Author

phra commented Dec 25, 2017

@SimenB i've implemented the tests and CI is green.. can you please review the code changes? thanks.

@SimenB
Copy link
Member

SimenB commented Dec 25, 2017

Why can't we use jest.spyOn? I don't understand why a new method is needed

@phra
Copy link
Contributor Author

phra commented Dec 27, 2017

in the end i just followed the original jasmine PR to be more compliant with the original library.

@SimenB as i said above, to maintain consistency with jasmine apis.

@SimenB
Copy link
Member

SimenB commented Dec 27, 2017

Parity with Jasmine's APIs is non-goal (we want to drop jasmine entirely, #4362).

@phra
Copy link
Contributor Author

phra commented Jan 4, 2018

@SimenB are we sure that changing behaviour of spyOn in this way will be retro compatible?

btw a benefit that i see valuable of having the same semantics of jasmine is letting people easily migrate from jasmine to jest without having to refactor the tests, that i consider a big sell point actually.

also, besides the fact that we want to drop jasmine entirely, why do you want to introduce a breaking change that is not strictly (i.e. technically) required?

@phra
Copy link
Contributor Author

phra commented Jan 8, 2018

@SimenB any feedback? i need this PR shipped to restore unit tests involving angularfire2, that starting from the latest major version it's exposing submodules via getters instead of normal properties.

@SimenB
Copy link
Member

SimenB commented Jan 8, 2018

@thymikee @cpojer I think this makes sense, WDYT?


if (uncheckedCount) {
uncheckedKeys = snapshotState.getUncheckedKeys();
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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 + '',
Copy link
Member

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: "?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will do it.

Copy link
Contributor Author

@phra phra Jan 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpojer in the end i've used getErrorMsg() function as in the spyOn function. is that better than manual format the string, isn'it?
see bd44ca5

}

if (!propertyName) {
throw new Error('No property name supplied');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will fix it.

@cpojer
Copy link
Member

cpojer commented Jan 8, 2018

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?

@phra
Copy link
Contributor Author

phra commented Jan 8, 2018

@cpojer good point. everything should work simply calling two times the spyOn method with get and set parameters, due to this line: https://github.com/phra/jest/blob/95f0ab9a6f6faa989e1590ba9695af854d30d7c7/packages/jest-jasmine2/src/jasmine/spy_registry.js#L197

what do you think about my reply?

@phra
Copy link
Contributor Author

phra commented Jan 8, 2018

@cpojer regarding the error messages formatting, in the end i've used getErrorMsg() function as in the spyOn function. is that better than manual format the string, isn'it?
see bd44ca5

@cpojer cpojer merged commit 490f435 into jestjs:master Jan 8, 2018
@phra phra deleted the feat/spy-on-property branch January 8, 2018 18:26
@phra
Copy link
Contributor Author

phra commented Jan 15, 2018

@SimenB @cpojer i don't see this feature reported in the changelog.. should i send a PR for that?

EDIT: btw i've finally restored my failing tests. 😄

@SimenB
Copy link
Member

SimenB commented Jan 15, 2018

@phra yes please!

phra added a commit to phra/jest that referenced this pull request Jan 15, 2018
`spyOn` in getters and setters is available starting from Jest v. 22.0.5, see jestjs#5107
@phra
Copy link
Contributor Author

phra commented Jan 15, 2018

@SimenB done! #5314

cpojer pushed a commit that referenced this pull request Jan 15, 2018
`spyOn` in getters and setters is available starting from Jest v. 22.0.5, see #5107
@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.

6 participants