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

Remove support for stubbing undefined props #1557

Merged
merged 1 commit into from Sep 18, 2017
Merged

Conversation

fatso83
Copy link
Contributor

@fatso83 fatso83 commented Sep 12, 2017

Purpose

This removes support for stubbing undefined properties, aligning the behavior with how sandbox stubbing works. The reasoning behind not allowing this is

stubbing non-existing own properties is likely to lead to mistakes in tests, where the test passes because the author mistyped the name of the existing property they were aiming to stub

Background

This was a longer discussion that can be found in the
issues #1508, #1552 and #1537 - and internally amongst the core team.

Support for stubbing props using get()/set() was added in #1237
and also worked for undefined properties.

This type of behavior (stubbing undefined props) has been
explicitly disabled for sandboxes, so in order for Sinon to
behave in a consistent way, we should do the same here.

This change will bring normal stubs back to how it has
used to be historically, and will make sandbox stubs
and normal sinon stubs behave the same.

It might break things for people relying on the behavior
that has been present since Sinon 2.0, but it should
make things more reliable going forward.

@fatso83
Copy link
Contributor Author

fatso83 commented Sep 12, 2017

The alternative to removing this functionality is the inverse of this:

@mroderick
Copy link
Member

You can count on me to pretty much always err on the side of stricter use of languages and apis, and this is another one of those cases.

I vote 👍

This was a longer discussion that can be found in the
issues sinonjs#1508, sinonjs#1552 and sinonjs#1537 - and internally amongst the core team.

Support for stubbing props using get()/set() was added in sinonjs#1237
and also worked for undefined properties.

This type of behavior (stubbing undefined props) has been
explicitly disabled for sandboxes, so in order for Sinon to
behave in a consistent way, we should do the same here.

This change will bring normal stubs back to how it has
used to be historically, and will make sandbox stubs
and normal sinon stubs behave the same.

It might break things for people relying on the behavior
that has been present since Sinon 2.0, but it should
make things more reliable going forward.
@mroderick
Copy link
Member

@sinonjs/sinon-core any objections to merging this?

@fatso83
Copy link
Contributor Author

fatso83 commented Sep 18, 2017

@mroderick Only the objections raised in this other issue. Thought it might be a nuisance to have two major releases in short succession, but it's not a big thing, so you might as well. There's not like there is a PR out there for the factory function refactor.

@mroderick
Copy link
Member

@mroderick Only the objections raised in this other issue. Thought it might be a nuisance to have two major releases in short succession, but it's not a big thing, so you might as well. There's not like there is a PR out there for the factory function refactor.

I don't see having several MAJOR releases as a big deal, as long as their changelogs are small at least :)

@Glogo
Copy link

Glogo commented Apr 23, 2018

This may be unrelated to this issue, but for those that have the same problem as me with version 4 now.
Instead of doing this:

sinon.stub(obj, 'yourMethod', () => Promise.resolve('whatever'))

you do:

sinon.stub(obj, 'yourMethod')
  .returns(() => Promise.resolve('whatever'));

@fatso83
Copy link
Contributor Author

fatso83 commented Apr 23, 2018

@Glogo, actually we have had promise support for several versions, so it is even shorter:

sinon.stub(obj, 'yourMethod').resolves('whatever');

@paulmelnikow
Copy link

stubbing non-existing own properties is likely to lead to mistakes in tests, where the test passes because the author mistyped the name of the existing property they were aiming to stub

Maybe; or maybe it's deliberate: #1537 (comment)

franck-romano pushed a commit to franck-romano/sinon that referenced this pull request Oct 1, 2019
This was a longer discussion that can be found in the
issues sinonjs#1508, sinonjs#1552 and sinonjs#1537 - and internally amongst the core team.

Support for stubbing props using get()/set() was added in sinonjs#1237
and also worked for undefined properties.

This type of behavior (stubbing undefined props) has been
explicitly disabled for sandboxes, so in order for Sinon to
behave in a consistent way, we should do the same here.

This change will bring normal stubs back to how it has
used to be historically, and will make sandbox stubs
and normal sinon stubs behave the same.

It might break things for people relying on the behavior
that has been present since Sinon 2.0, but it should
make things more reliable going forward.
@cphoover
Copy link

cphoover commented Jan 3, 2020

Why was support for this removed?

As others have said adding

window.someKey = 'someValue';
// ... testCode here
delete window.someKey

is problematic if you have multiple differing values between tests, or it may be defined as something else in the future. With sandbox.restore we know we cleanup after ourselves so if that value is defined in the future we shouldn't run into issues. I think it makes sense to allow stubbing undefined properties maybe add another method if you are worried about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver:major changes will cause a new major version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants