Skip to content

Commit

Permalink
Revert spied fakeTimers to original state
Browse files Browse the repository at this point in the history
When sandbox.useFakeTimers() is called, the original setTimeout and
other methods are replaced with fake methods. When such fake method is
spied it's replaced again with another fake. To make sure that
sandbox.restore() reverts setTimeout and other methods to the original
state these fakes should be restored in reverse order to make sure each
fake is restored to its predecessor when calling fake.restore().
  • Loading branch information
ig-pj committed May 21, 2018
1 parent e2c7132 commit 8b6f8a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sinon/sandbox.js
Expand Up @@ -132,7 +132,7 @@ function Sandbox() {
throw new Error("sandbox.restore() does not take any parameters. Perhaps you meant stub.restore()");
}

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

forEach.call(fakeRestorers, function (restorer) {
Expand Down
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 8b6f8a8

Please sign in to comment.