Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1852: Add a way to pass a global context to lolex when calling useFakeTimers #1935

Merged
merged 1 commit into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});