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

Docs: add details about (not) stubbing non-existing own properties #1722

Conversation

mroderick
Copy link
Member

This should help remove frustration for end users and attract fewer
GitHub issues.

Once we are happy with the details in the changes to release-source, I'll update the PR to also update the existing releases.

This should help remove frustration for end users and attract fewer
GitHub issues.
@@ -157,6 +157,19 @@ console.log(myObject.hello);
// world
```

##### "Cannot stub non-existent own property"
Copy link
Contributor

@fatso83 fatso83 Mar 8, 2018

Choose a reason for hiding this comment

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

Hmm ... has this changed without me noticing? I thought we at some point prevented stubbing properties that were higher on the prototype chain, but an experiment seems this isn't happening. This makes the error a bit weird. Then we should just throw "Cannot stub non-existing property". I might be missing something though.

function SuperAPI(){}
SuperAPI.prototype.aMethod = function (){ console.log('super'); }

var myAPI = Object.create(SuperAPI.prototype);
console.log(Object.getOwnPropertyNames(myAPI)) // => '[]'

stub = sinon.stub(myAPI, 'aMethod'); // no error
console.log(Object.getOwnPropertyNames(myAPI)) // => '["aMethod"]'

Copy link
Member Author

Choose a reason for hiding this comment

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

Yours is a good example, this is confusing behaviour, not to say the least that it creates local properties, that will shadow the properties in the prototype.

if (object && typeof property !== "undefined" && !Object.prototype.hasOwnProperty.call(object, property)) {

Changed in b4a3f42 to

if (object && typeof property !== "undefined" && !(property in object)) {

I think we need some better tests for the expected behaviour around stubbing non-existent own properties ... and a fix for this bug.

Do you want to create an issue yourself?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, that's it. It used to work to stub stuff on the prototype, then it didn't (for sandboxes), and then the regression was fixed (by me - I forgot 😄). But I am not sure about the bug. What is the bug; lack of documentation? Or do you think sinon should run up the prototype until it finds the prop somewhere? I am not sure what the confusing behavior is, just that the error is confusing.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the first implementation was the correct one, where we used hasOwnProperty. If you want to stub something on a prototype, then you should target the prototype. Just running up the prototype chain and replacing methods is likely to have unforeseen consequences.

I think we should revert to using hasOwnProperty.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mroderick There is something very weird that has been going on, and I think I am guilty of whatever has been taking place, but I am not totally sure what has been going on. See the time-related, but totally opposing PR of #1516 and #1557. To me it seems we in a matter of a month inversed the support for stubbing on the prototype, though the intent was to align the support for normal stubs and sandboxed stubs. What was supported => removed support, and vice versa.

Am I right? I got totally confused by reading the discussions, as they went a bit back and forth.

No matter which solution we end up with, it will make some people annoyed and end up in new bug reports, as it will revert some functionality (again) that worked, then didn't, then did ... Oh, well. Time for a new major anyway.

@stale
Copy link

stale bot commented May 8, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 8, 2018
@stale stale bot closed this May 15, 2018
@mroderick mroderick deleted the document-stub-non-existent-property branch May 15, 2018 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants