Skip to content

Commit

Permalink
Issue #1852: Add a way to pass a global context to lolex when calling…
Browse files Browse the repository at this point in the history
… useFakeTimers
  • Loading branch information
LouisBrunner authored and mroderick committed Oct 25, 2018
1 parent ae80fdc commit 60fb06e
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 60fb06e

Please sign in to comment.