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 390 implicit dependencies on faking interval #391

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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