From 60fb06e3cf143320c6465e501ade5109cd235e74 Mon Sep 17 00:00:00 2001 From: Louis Brunner Date: Mon, 22 Oct 2018 23:07:36 +0100 Subject: [PATCH] Issue #1852: Add a way to pass a global context to lolex when calling useFakeTimers --- lib/sinon/util/fake-timers.js | 13 ++++++++++--- test/util/fake-timers-test.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/sinon/util/fake-timers.js b/lib/sinon/util/fake-timers.js index 5476b2c97..bfb508bf3 100644 --- a/lib/sinon/util/fake-timers.js +++ b/lib/sinon/util/fake-timers.js @@ -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; } @@ -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"); diff --git a/test/util/fake-timers-test.js b/test/util/fake-timers-test.js index 6457c85a2..8fddfe1e6 100644 --- a/test/util/fake-timers-test.js +++ b/test/util/fake-timers-test.js @@ -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(); + }); }); });