Skip to content

Commit

Permalink
Issue 390 implicit dependencies on faking interval (#391)
Browse files Browse the repository at this point in the history
All in all, this makes the code a lot easier to read/understand. There was unneeded complexity earlier.

* Create verification test for #390
* Fix issue #390: remove dependency on faking setInterval
  • Loading branch information
fatso83 committed Jun 21, 2021
1 parent cc30595 commit 604f090
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
49 changes: 23 additions & 26 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,12 +803,6 @@ function withGlobal(_global) {
} else {
if (_global[method] && _global[method].hadOwnProperty) {
_global[method] = clock[`_${method}`];
if (
method === "clearInterval" &&
config.shouldAdvanceTime === true
) {
_global[method](clock.attachedInterval);
}
} else {
try {
delete _global[method];
Expand All @@ -819,6 +813,10 @@ function withGlobal(_global) {
}
}

if (config.shouldAdvanceTime === true) {
_global.clearInterval(clock.attachedInterval);
}

// Prevent multiple executions which will completely remove these props
clock.methods = [];

Expand Down Expand Up @@ -1602,38 +1600,37 @@ function withGlobal(_global) {
});
}

if (config.shouldAdvanceTime === true) {
const intervalTick = doIntervalTick.bind(
null,
clock,
config.advanceTimeDelta
);
const intervalId = _global.setInterval(
intervalTick,
config.advanceTimeDelta
);
clock.attachedInterval = intervalId;
}

for (i = 0, l = clock.methods.length; i < l; i++) {
if (clock.methods[i] === "hrtime") {
const nameOfMethodToReplace = clock.methods[i];
if (nameOfMethodToReplace === "hrtime") {
if (
_global.process &&
typeof _global.process.hrtime === "function"
) {
hijackMethod(_global.process, clock.methods[i], clock);
hijackMethod(_global.process, nameOfMethodToReplace, clock);
}
} else if (clock.methods[i] === "nextTick") {
} else if (nameOfMethodToReplace === "nextTick") {
if (
_global.process &&
typeof _global.process.nextTick === "function"
) {
hijackMethod(_global.process, clock.methods[i], clock);
hijackMethod(_global.process, nameOfMethodToReplace, clock);
}
} else {
if (
clock.methods[i] === "setInterval" &&
config.shouldAdvanceTime === true
) {
const intervalTick = doIntervalTick.bind(
null,
clock,
config.advanceTimeDelta
);
const intervalId = _global[clock.methods[i]](
intervalTick,
config.advanceTimeDelta
);
clock.attachedInterval = intervalId;
}
hijackMethod(_global, clock.methods[i], clock);
hijackMethod(_global, nameOfMethodToReplace, clock);
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3955,6 +3955,24 @@ describe("FakeTimers", function () {
}
}, interval);
});

it("should not depend on having to stub setInterval or clearInterval to work", function (done) {
const origSetInterval = globalObject.setInterval;
const origClearInterval = globalObject.clearInterval;

var clock = FakeTimers.install({
shouldAdvanceTime: true,
toFake: ["setTimeout"],
});

assert.equals(globalObject.setInterval, origSetInterval);
assert.equals(globalObject.clearInterval, origClearInterval);

setTimeout(function () {
clock.uninstall();
done();
}, 0);
});
});

describe("requestAnimationFrame", function () {
Expand Down

0 comments on commit 604f090

Please sign in to comment.