Skip to content

Commit

Permalink
Just use withGlobal(target) instead.
Browse files Browse the repository at this point in the history
See sinonjs#276
for background and/or sinonjs/sinon#2156
  • Loading branch information
fatso83 committed Mar 25, 2020
1 parent 480f68f commit 4ac4e5b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
8 changes: 3 additions & 5 deletions integration-test/fake-clock-integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("withGlobal", function() {
});

it("should support basic setTimeout", function() {
var clock = withGlobal.install({ target: jsdomGlobal, toFake: timers });
var clock = withGlobal.install({ toFake: timers });
var stub = sinon.stub();

jsdomGlobal.setTimeout(stub, 5);
Expand All @@ -50,7 +50,7 @@ describe("withGlobal", function() {
it("Date is instanceof itself", function() {
assert(new jsdomGlobal.Date() instanceof jsdomGlobal.Date);

var clock = withGlobal.install({ target: jsdomGlobal, toFake: timers });
var clock = withGlobal.install({ toFake: timers });

assert(new jsdomGlobal.Date() instanceof jsdomGlobal.Date);

Expand Down Expand Up @@ -78,9 +78,7 @@ describe("globally configured browser objects", function() {

global.window = window;
global.document = window.document;
global.navigator = {
userAgent: "node.js"
};
global.navigator = window.navigator;
global.requestAnimationFrame = function(callback) {
return setTimeout(callback, 0);
};
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 26 additions & 22 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,17 +466,17 @@ function withGlobal(_global) {
}
}

function uninstall(clock, target, config) {
function uninstall(clock, config) {
var method, i, l;
var installedHrTime = "_hrtime";
var installedNextTick = "_nextTick";

for (i = 0, l = clock.methods.length; i < l; i++) {
method = clock.methods[i];
if (method === "hrtime" && target.process) {
target.process.hrtime = clock[installedHrTime];
} else if (method === "nextTick" && target.process) {
target.process.nextTick = clock[installedNextTick];
if (method === "hrtime" && _global.process) {
_global.process.hrtime = clock[installedHrTime];
} else if (method === "nextTick" && _global.process) {
_global.process.nextTick = clock[installedNextTick];
} else if (method === "performance") {
var originalPerfDescriptor = Object.getOwnPropertyDescriptor(
clock,
Expand All @@ -488,25 +488,25 @@ function withGlobal(_global) {
!originalPerfDescriptor.set
) {
Object.defineProperty(
target,
_global,
method,
originalPerfDescriptor
);
} else if (originalPerfDescriptor.configurable) {
target[method] = clock["_" + method];
_global[method] = clock["_" + method];
}
} else {
if (target[method] && target[method].hadOwnProperty) {
target[method] = clock["_" + method];
if (_global[method] && _global[method].hadOwnProperty) {
_global[method] = clock["_" + method];
if (
method === "clearInterval" &&
config.shouldAdvanceTime === true
) {
target[method](clock.attachedInterval);
_global[method](clock.attachedInterval);
}
} else {
try {
delete target[method];
delete _global[method];
} catch (ignore) {
/* eslint no-empty: "off" */
}
Expand Down Expand Up @@ -1220,7 +1220,6 @@ function withGlobal(_global) {

/**
* @param config {Object} optional config
* @param config.target {Object} the target to install timers in (default `window`)
* @param config.now {number|Date} a number (in milliseconds) or a Date object (default epoch)
* @param config.toFake {string[]} names of the methods that should be faked.
* @param config.loopLimit {number} the maximum number of timers that will be run when calling runAll()
Expand All @@ -1247,12 +1246,17 @@ function withGlobal(_global) {
config.shouldAdvanceTime = config.shouldAdvanceTime || false;
config.advanceTimeDelta = config.advanceTimeDelta || 20;

if (config.target) {
throw new TypeError(
"config.target is no longer supported. Use `withGlobal(target)` instead."
);
}

var i, l;
var target = config.target || _global;
var clock = createClock(config.now, config.loopLimit);

clock.uninstall = function() {
return uninstall(clock, target, config);
return uninstall(clock, config);
};

clock.methods = config.toFake || [];
Expand All @@ -1267,17 +1271,17 @@ function withGlobal(_global) {
for (i = 0, l = clock.methods.length; i < l; i++) {
if (clock.methods[i] === "hrtime") {
if (
target.process &&
typeof target.process.hrtime === "function"
_global.process &&
typeof _global.process.hrtime === "function"
) {
hijackMethod(target.process, clock.methods[i], clock);
hijackMethod(_global.process, clock.methods[i], clock);
}
} else if (clock.methods[i] === "nextTick") {
if (
target.process &&
typeof target.process.nextTick === "function"
_global.process &&
typeof _global.process.nextTick === "function"
) {
hijackMethod(target.process, clock.methods[i], clock);
hijackMethod(_global.process, clock.methods[i], clock);
}
} else {
if (
Expand All @@ -1289,13 +1293,13 @@ function withGlobal(_global) {
clock,
config.advanceTimeDelta
);
var intervalId = target[clock.methods[i]](
var intervalId = _global[clock.methods[i]](
intervalTick,
config.advanceTimeDelta
);
clock.attachedInterval = intervalId;
}
hijackMethod(target, clock.methods[i], clock);
hijackMethod(_global, clock.methods[i], clock);
}
}

Expand Down
8 changes: 8 additions & 0 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4593,3 +4593,11 @@ describe("FakeTimers", function() {
});
});
});

describe("#276 - remove config.target", function() {
it("should throw on using `config.target`", function() {
assert.exception(function() {
FakeTimers.install({ target: {} });
}, /config.target is no longer supported/);
});
});

0 comments on commit 4ac4e5b

Please sign in to comment.