Skip to content

Commit

Permalink
Issue sinonjs#1852: Add a way to pass a global context to lolex when …
Browse files Browse the repository at this point in the history
…calling useFakeTimers
  • Loading branch information
LouisBrunner committed Oct 22, 2018
1 parent 9ed8b8f commit 8241f38
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/sinon/util/fake-timers.js
Expand Up @@ -3,8 +3,12 @@
var extend = require("./core/extend");
var llx = require("lolex");

function createClock(config) {
var clock = llx.install(config);
function createClock(config, globalCtx) {
var llxCtx = llx;
if (globalCtx !== null && typeof globalCtx === "object") {
llxCtx = llx.withGlobal(globalCtx);
}
var clock = llxCtx.install(config);
clock.restore = clock.uninstall;
return clock;
}
Expand Down Expand Up @@ -32,7 +36,10 @@ exports.useFakeTimers = function(dateOrConfig) {
}

if (argumentIsObject) {
return createClock(extend({}, dateOrConfig));
var config = extend({}, dateOrConfig);
var globalCtx = config.global;
delete config.global;
return createClock(config, globalCtx);
}

throw new TypeError("useFakeTimers expected epoch or config object. See https://github.com/sinonjs/sinon");
Expand Down
13 changes: 13 additions & 0 deletions test/util/fake-timers-test.js
Expand Up @@ -1183,5 +1183,18 @@ describe("fakeTimers.clock", function() {
{ name: "TypeError", message: expectedError }
);
});

it("supports a way to pass the global context", function() {
var stub = sinonStub.create();
var globalCtx = {
Date: sinonStub.create(),
setTimeout: stub,
clearTimeout: sinonStub.create()
};
this.clock = fakeTimers.useFakeTimers({ global: globalCtx });
refute.defined(this.clock.performance);
assert.same(this.clock._setTimeout, stub); // eslint-disable-line no-underscore-dangle
this.clock.restore();
});
});
});

0 comments on commit 8241f38

Please sign in to comment.