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 spies for get/set functions on accessor properties #943

Closed
apsillers opened this issue Oct 16, 2015 · 26 comments
Closed

Implement spies for get/set functions on accessor properties #943

apsillers opened this issue Oct 16, 2015 · 26 comments

Comments

@apsillers
Copy link

I think it would be useful to extend spyOn (or add another method, like spyOnGetter) to spy on the getter or setter function of an accessor property. Currently there is no way to spy on the following get functions, because Jasmine currently can only spy on functions that are properties, and not on functions that are set/get accessor functions of a property:

Object.defineProperty(obj, "dynamicFoo", {
    configurable: true,
    get: function() {
        return Math.random();
    }
});

var obj = {
    get dyanmicBar() { return Math.random(); }
}

It seems like the core functionality could be implemented using defineProperty to rewrite the property with a spy for its get or set method.

The current code is

this.spyOn = function(obj, methodName) {
    ...
    var spy = j$.createSpy(methodName, obj[methodName]);
    obj[methodName] = spy;
}

The code could handle accessor functions with a third argument (either "get" or "set") like so:

this.spyOn = function(obj, methodName, accessType) {
    ...
    var desc = Object.getPropertyDescriptor(obj, methodName);
    if(desc[accessType]) { // "get" or "set" exists on the property
        var spy = j$.createSpy(methodName, desc[accessType]);  

        desc[accessType] = spy;

        Object.defineProperty(obj, methodName, desc);
    }
}

However, I'm not sure how this would break existing assumptions. For example, in my sample code above, createSpy is given the property name, but the property is not being spied on; rather, one of the property's accessor functions is being spied on. This change would require eliminating the assumption that a function being spied on is a property of an object, and instead writing logic that admits the possibility that a spied function could be a get or set function within an object property.

@StanleyGoldman
Copy link

+1

@slackersoft
Copy link
Member

I can see that being useful. I don't think we want to add that complexity to the existing spyOn implementation, but I would be happy to review a pull request to add something like spyOnProperty or something.

@mgilliganrca
Copy link

+1

@isadovskiy
Copy link

+1

11 similar comments
@jamesbrobb
Copy link

+1

@apaatsio
Copy link

apaatsio commented Feb 8, 2016

+1

@maxyharr
Copy link

+1

@vojtech-cerveny
Copy link

+1

@salickc
Copy link

salickc commented Mar 16, 2016

+1

@ghost
Copy link

ghost commented Mar 18, 2016

+1

@nicbou
Copy link

nicbou commented Mar 23, 2016

+1

@anjmao
Copy link

anjmao commented Mar 31, 2016

+1

@FunkeyFlo
Copy link

+1

@benediktarnold
Copy link

+1

@chrisatanasian
Copy link

+1

@ptomato
Copy link
Contributor

ptomato commented May 28, 2016

I think at this point the +1s are crowding out any actual discussion that might happen here; please show your enthusiasm by subscribing yourself to the issue or adding a 👍 reaction with GitHub's new "reaction" feature so that everybody doesn't continue to get notifications for each new +1.

Or, better yet, attach a pull request 😄

@henrahmagix
Copy link
Contributor

@ptomato #1008 is a pull-request that fulfills this issue, already attached

@Gigitsu
Copy link

Gigitsu commented Jun 28, 2016

+1

@henrahmagix
Copy link
Contributor

@slackersoft We now have #1008 and #1203 able to be merged. Is there a timeline for this feature?

@elwynelwyn
Copy link

Any guidance on when this will make it into a release?

@joshfermin
Copy link

+1

1 similar comment
@fimius23
Copy link

+1

@henrahmagix
Copy link
Contributor

@elwynelwyn @joshfermin @fimius23 I know this is late so you may know already, but this is available in 2.6 (#1203 (comment)) with docs at https://jasmine.github.io/api/edge/global.html#spyOnProperty

@pauljeffreys
Copy link

I realise I am late to the game here, but does anybody know of a similar function call for jest? Wanting to test a getter in that also, but this addition is just jasmine..

@slackersoft
Copy link
Member

@pauljeffreys you should probable ask the Jest folks. (https://github.com/facebook/jest) I'm not totally sure it's even still built on top of Jasmine in a way that would allow them to incorporate the work done here.

@phra
Copy link

phra commented Dec 18, 2017

@pauljeffreys @slackersoft see my PR at jestjs/jest#5107

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests