-
-
Notifications
You must be signed in to change notification settings - Fork 769
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
Conditional support for replacing props not defined by the object #2195
Comments
As I understand this, As I understand the argument for allowing to stub With this in mind, I would like to propose an alternative to the suggested sandbox option here: sinon.define(child, 'foo', sinon.fake()); I see these advantages:
What do you think? |
I agree that it's unfortunate that
I am not a fan of configuration, as that has bitten me in the past. PHP was notoriously difficult to maintain in the past, because the behaviour of functions and APIs changed when you made changes to Having a flag that changes behaviour, means that you can't look at some code and be certain you understand what it does. I think the |
Well, that does not really change anything about the feature disparity (being able to P.S. The |
You could always do |
OK, with that in mind, then |
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. |
@fatso83 do you agree with the bot closing this issue? |
Not sure. Maybe it should have been recreated as "add a way to define fakes for undefined props", to reflect our discussion. That was a good suggestion, but my original gripe - lack of alignment in behaviour didn't seem to end up in anything. Still thinks it is in unfortunate that we have different ways to handle proto props and/or undefined props, depending on which fake implementation you choose. |
I agree that |
never supported undefined or protypal props in the first place. See sinonjs#2195 for backing discussion on creating sinon.define()
never supported undefined or protypal props in the first place. See sinonjs#2195 for backing discussion on creating sinon.define()
* regression test for #2491 * fix: ensure we can always restore our own spies * Add tests for mocks, fakes and stubs This shows an incoherent appraoch to how we deal with object descriptors across different code paths. * fix: only bother with unconfigurable descriptors if they are our own * remove test for sandbox.replace never supported undefined or protypal props in the first place. See #2195 for backing discussion on creating sinon.define() * fix linting
One more vote for this feature and the |
Is your feature request related to a problem? Please describe.
In #1557 we removed support for stubbing undefined properties, aligning the behavior with how sandbox stubbing worked. Later on we have made the sinon methods use a default sandbox to further align how the default methods and sandboxes worked.
Still, as the thread in #1537 show, this remains a sorely missed feature for some, and while we had good reasons for removing it, maybe we could appease the ones wanting it by making it an opt-in? It remains a quite useful feature in some cases.
Describe the solution you'd like
I suggest adding a config field to the sandbox to allow defining fields (fakes) that are not originally defined by the objects we insert the fake into. Something like
sinon.createSandbox({allowUndefined: true})
? (Totally not sure about the actual name of the prop, though ...allowNonOwn
?)Given
it should allow
Related to this, but really a different case, is props on the prototype chain. As explained in the "Solution" paragraph in #2193 we deal with this differently today, depending on whether we use spies or fakes:
If we simplified the "undefined" and "props on an ancestor" problems into a single issue: props not defined by the object whose fields are being stubbed, we could align this behavior for our spies, stubs and fakes under a default non-permissive line and have an opt-in solution for those who want it. By default, you would then only be allowed to replace fields owned/defined by the object.
Having the opt-in for stubs and spies would not be a breaking change, but as the
sinon.replace
method today allows stubbing defined props owned by an ancestor it would be a breaking change to disallow this by default.Describe alternatives you've considered
Two flags instead of one:
allowUndefinedProps
(props are not present anywhere on the prototype chain) andallowShadowingFieldsOnPrototype
(to stub fields defined by ancestors). I don't really see the benefit, though.The text was updated successfully, but these errors were encountered: