-
-
Notifications
You must be signed in to change notification settings - Fork 771
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 restoring getters/setters/values for previously unexisting props #1419
Fix restoring getters/setters/values for previously unexisting props #1419
Conversation
This looks fine to me, but I have a question: Sinon currently throws when trying to stub a non-existing function. Shouldn't the same be true for unknown properties? If so, cleaning up undefined properties wouldn't be necessary because it shouldn't happen in the first place, right? If we conclude that adding previously undefined properties with Sinon is fine, this fixes an issue and should be a patch. |
👍 |
@mantoni, thanks for your review, but actually sinon does allow users to stub undefined properties, thus this is just a This happens because of this check inside The piece responsible for throwing an error is This PR is focused at fixing the behaviors that make sense to be used with non-function properties, which are mainly IMO another PR is needed to allow stubbing Thanks everyone for your input. |
@lucasfcosta Thanks for elaborating. I was trying to point out that there is an inconsistency between function stubbing and property stubbing. I do like the exception that is being thrown when I try to stub a non-existing function because it catches my typos while writing tests. My question is not whether we should remove that check, it's whether we should introduce it for property stubbing as well? |
@mantoni I think that it's impossible to throw when stubbing properties that are undefined only when we want them to have their "call behavior" changed (as it happens when using IMO we should add relevant throws to |
@lucasfcosta Yeah, I don't have a strong opinion here. It makes sense to have different semantics for properties and functions for the reasons you mentioned. |
@mantoni Thanks for your review and for sharing your opinion, I agree with what you've said when it comes to stub functions. I'll see what I can do regarding those |
This has become part of |
Seems like we have introduced a regression (#1456). |
Purpose (TL;DR)
This allows our users to restore
getters
,setters
andvalues
(property descriptor's properties) for properties that were previously undefined.It also removes the unnecessary
restore
method inside thevalue
behavior introduced in #1416 since we can use the stub's own restore method to accomplish the goal of restoring avalue
.This is a remake of #1418 because of further problems I discovered while playing with that solution.
Background (Problem in detail)
This happened because we were always getting the actualDescriptor of the property being stubbed, thus, if this property was not defined,
actualDescriptor
would be undefined.This caused the
restore
method to throw an error when usingObject.defineProperty
to redefine that property back to its old value becauseundefined
was passed to it as the descriptor.Solution
In order to solve this I added a check to see if the descriptor we've got inside the
stub
closure is defined. If it is not defined we can just delete the stubbed property.In order to be able to delete it, that property must be
configurable
so I had to add aconfigurable: true
property to the property descriptors passed when defining a new getter, a new setter and a new value for a property.Also, noew the
value
behavior also defines anenumerable
property, because that way it behaves more like a property defined using an assignment operator (=
).This PR also includes tests for all those changes, one for each default behavior that has been modified.
How to verify
npm install
npm test
PS: Sorry for opening that other unuseful PR before this one. 😓