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

Fix #1801: spied fakeTimers are not restored correctly #1802

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/sinon.js
Expand Up @@ -24,11 +24,8 @@ var apiMethods = {
setFormatter: format.setFormatter,

// fake timers
useFakeTimers: fakeTimers.useFakeTimers,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So these lines that were hanging around from earlier versions essentially shadowed the new sandbox functions on the prototype. Good find!

clock: fakeTimers.clock,
timers: fakeTimers.timers,


// fake XHR
xhr: nise.fakeXhr.xhr,
FakeXMLHttpRequest: nise.fakeXhr.FakeXMLHttpRequest,
Expand Down
2 changes: 2 additions & 0 deletions lib/sinon/sandbox.js
Expand Up @@ -17,6 +17,7 @@ var fakeXhr = require("nise").fakeXhr;
var usePromiseLibrary = require("./util/core/use-promise-library");

// cache original versions, to prevent issues when they are stubbed in user space
var reverse = Array.prototype.reverse;
var push = Array.prototype.push;
var filter = Array.prototype.filter;
var forEach = Array.prototype.filter;
Expand Down Expand Up @@ -132,6 +133,7 @@ function Sandbox() {
throw new Error("sandbox.restore() does not take any parameters. Perhaps you meant stub.restore()");
}

reverse.call(collection);
applyOnEach(collection, "restore");
collection = [];

Expand Down
13 changes: 13 additions & 0 deletions test/issues/issues-test.js
Expand Up @@ -466,4 +466,17 @@ describe("issues", function () {
// TypeError: Attempted to wrap someMethod which is already wrapped
});
});

describe("#1801 - sinon.restore spied fakeTimers", function () {
it("should restore spied fake timers", function () {
var originalSetTimeout = setTimeout;

sinon.useFakeTimers();
sinon.spy(global, "setTimeout");

sinon.restore();

assert.same(originalSetTimeout, global.setTimeout, "fakeTimers restored");
});
});
});
11 changes: 11 additions & 0 deletions test/sandbox-test.js
Expand Up @@ -933,6 +933,17 @@ describe("Sandbox", function () {

assert.same(setTimeout, originalSetTimeout, "fakeTimers restored");
});

it("restores spied fake timers when then sanddox is restored", function () {
var originalSetTimeout = setTimeout;

this.sandbox.useFakeTimers();
this.sandbox.spy(global, "setTimeout");

this.sandbox.restore();

assert.same(originalSetTimeout, global.setTimeout, "fakeTimers restored");
});
});

describe(".usingPromise", function () {
Expand Down