Skip to content

Commit

Permalink
Merge pull request #1802 from ifrost/feature/restore-default-sandbox-…
Browse files Browse the repository at this point in the history
…fake-timers

Fix  #1801: spied fakeTimers are not restored correctly

closes #1780
  • Loading branch information
fatso83 committed May 24, 2018
2 parents 86b930c + 087bc1c commit 5ca48d3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
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,
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

0 comments on commit 5ca48d3

Please sign in to comment.