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

getTimerCount will not include cancelled immediates #8764

Merged
merged 9 commits into from
Aug 18, 2019
31 changes: 10 additions & 21 deletions packages/jest-fake-timers/src/jestFakeTimers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const setGlobal = (
};

export default class FakeTimers<TimerRef> {
private _cancelledImmediates!: Record<string, boolean>;
private _cancelledTicks!: Record<string, boolean>;
private _config: StackTraceConfig;
private _disposed?: boolean;
Expand Down Expand Up @@ -105,9 +104,7 @@ export default class FakeTimers<TimerRef> {
}

clearAllTimers() {
this._immediates.forEach(immediate =>
this._fakeClearImmediate(immediate.uuid),
);
this._immediates = [];
this._timers.clear();
}

Expand All @@ -118,7 +115,6 @@ export default class FakeTimers<TimerRef> {

reset() {
this._cancelledTicks = {};
this._cancelledImmediates = {};
this._now = 0;
this._ticks = [];
this._immediates = [];
Expand Down Expand Up @@ -177,11 +173,8 @@ export default class FakeTimers<TimerRef> {
}

private _runImmediate(immediate: Tick) {
if (!this._cancelledImmediates.hasOwnProperty(immediate.uuid)) {
// Callback may throw, so update the map prior calling.
this._cancelledImmediates[immediate.uuid] = true;
immediate.callback();
}
immediate.callback();
jeysal marked this conversation as resolved.
Show resolved Hide resolved
this._fakeClearImmediate(immediate.uuid);
}

runAllTimers() {
Expand Down Expand Up @@ -355,11 +348,7 @@ export default class FakeTimers<TimerRef> {
getTimerCount() {
this._checkFakeTimers();

const immediatesCount = this._immediates.filter(
immediate => !this._cancelledImmediates[immediate.uuid],
).length;

return this._timers.size + immediatesCount + this._ticks.length;
return this._timers.size + this._immediates.length + this._ticks.length;
}

private _checkFakeTimers() {
Expand Down Expand Up @@ -406,7 +395,9 @@ export default class FakeTimers<TimerRef> {
}

private _fakeClearImmediate(uuid: TimerID) {
this._cancelledImmediates[uuid] = true;
this._immediates = this._immediates.filter(
immediate => immediate.uuid !== uuid,
);
}

private _fakeNextTick(callback: Callback, ...args: Array<any>) {
Expand Down Expand Up @@ -443,13 +434,11 @@ export default class FakeTimers<TimerRef> {
uuid: String(uuid),
});

const cancelledImmediates = this._cancelledImmediates;
const immediates = this._immediates;
this._timerAPIs.setImmediate(() => {
if (!cancelledImmediates.hasOwnProperty(uuid)) {
// Callback may throw, so update the map prior calling.
cancelledImmediates[String(uuid)] = true;
this._fakeClearImmediate(String(uuid));
immediates.find(x => x.uuid === String(uuid)) &&
eranshabi marked this conversation as resolved.
Show resolved Hide resolved
callback.apply(null, args);
}
});

return uuid;
Expand Down