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

Default sandbox's restore method does not restore stubs/spies #1775

Closed
ndhoule opened this issue May 1, 2018 · 4 comments
Closed

Default sandbox's restore method does not restore stubs/spies #1775

ndhoule opened this issue May 1, 2018 · 4 comments

Comments

@ndhoule
Copy link

ndhoule commented May 1, 2018

When using any sandbox method on the default sandbox, restore() does not restore mocks or spies properly. A minimal repro case on Sinon v5.0.2:

const sinon = require('sinon');
const myApi = {
  someMethod() {
    console.log('test method!');
  }
};

sinon.stub(myApi, 'someMethod');
sinon.restore();
sinon.stub(myApi, 'someMethod');
// TypeError: Attempted to wrap someMethod which is already wrapped

It seems that in the call to extend in lib/sinon.js, the un-sandboxed stub/spy/etc. exports from apiMethods end up shadowing the sandbox methods, and so restore() doesn't do anything. The Sandbox API docs and source in lib/sinon.js seem to suggest that this is intended usage.

I only tested stub and spy, but I would expect any other shadowed methods also do not work as intended.

I ran these tests on Node v8.9.4 and Chrome 66.0.3359.117 via Karma/Chromedriver, though this issue seems unrelated to environment.

mroderick added a commit to mroderick/sinon that referenced this issue May 2, 2018
This was caused by adding stub, spy, mock explicitly to apiMethods, thus
shadowing the methods from Sanbox, which are the ones that collect the
fakes for restoring.

Removing these methods, allows the caller to use the sandbox methods,
and all is well.
@mroderick
Copy link
Member

Thank you for your detailed error report 👍

@mroderick
Copy link
Member

This has been fixed in sinon@5.0.3

@ndhoule
Copy link
Author

ndhoule commented May 2, 2018

Thanks for addressing this so quickly! Much appreciated :)

@mroderick
Copy link
Member

With good error reports, most bugs are easy to squash

franck-romano pushed a commit to franck-romano/sinon that referenced this issue Oct 1, 2019
This was caused by adding stub, spy, mock explicitly to apiMethods, thus
shadowing the methods from Sanbox, which are the ones that collect the
fakes for restoring.

Removing these methods, allows the caller to use the sandbox methods,
and all is well.
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

2 participants