Skip to content

Commit

Permalink
Fix #1775: Default sandbox does not restore stubs, spies, mocks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mroderick committed May 2, 2018
1 parent 724dd6c commit 2a3696d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
3 changes: 0 additions & 3 deletions lib/sinon.js
Expand Up @@ -15,10 +15,7 @@ var apiMethods = {
assert: require("./sinon/assert"),
fake: require("./sinon/fake"),
match: require("./sinon/match"),
spy: require("./sinon/spy"),
spyCall: require("./sinon/call"),
stub: require("./sinon/stub"),
mock: require("./sinon/mock"),

expectation: require("./sinon/mock-expectation"),
createStubInstance: require("./sinon/stub").createStubInstance,
Expand Down
5 changes: 5 additions & 0 deletions lib/sinon/sandbox.js
Expand Up @@ -2,6 +2,7 @@

var collectOwnMethods = require("./collect-own-methods");
var getPropertyDescriptor = require("./util/core/get-property-descriptor");
var isEsModule = require("./util/core/is-es-module");
var isPropertyConfigurable = require("./util/core/is-property-configurable");
var isNonExistentOwnProperty = require("./util/core/is-non-existent-own-property");
var sinonMatch = require("./match");
Expand Down Expand Up @@ -258,6 +259,10 @@ function Sandbox() {
};

sandbox.stub = function stub(object, property) {
if (isEsModule(object)) {
throw new TypeError("ES Modules cannot be stubbed");
}

if (isNonExistentOwnProperty(object, property)) {
throw new TypeError("Cannot stub non-existent own property " + valueToString(property));
}
Expand Down
46 changes: 46 additions & 0 deletions test/issues/issues-test.js
Expand Up @@ -420,4 +420,50 @@ describe("issues", function () {
refute.isTrue(spyProp.get.called);
});
});

// this error was caused by overwriting methods with imported ones don't use the collection
// and thus were not restorable
describe("#1775 - sinon.restore", function () {
it("should restore all stubs", function () {
var myApi = {
someMethod: function someMethod() {
// eslint-disable-next-line no-console
console.log("test method!");
}
};

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

it("should restore all spies", function () {
var myApi = {
someMethod: function someMethod() {
// eslint-disable-next-line no-console
console.log("test method!");
}
};

sinon.spy(myApi, "someMethod");
sinon.restore();
sinon.spy(myApi, "someMethod");
// TypeError: Attempted to wrap someMethod which is already wrapped
});

it("should restore all mocks", function () {
var myApi = {
someMethod: function someMethod() {
// eslint-disable-next-line no-console
console.log("test method!");
}
};

sinon.mock(myApi);
sinon.restore();
sinon.mock(myApi);
// TypeError: Attempted to wrap someMethod which is already wrapped
});
});
});

0 comments on commit 2a3696d

Please sign in to comment.