Skip to content

Commit d69f8d3

Browse files
eranshabiSimenB
authored andcommitted
getTimerCount will not include cancelled immediates (#8764)
1 parent b4bd77b commit d69f8d3

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

7+
- `[jest-fake-timers]` `getTimerCount` will not include cancelled immediates ([#8764](https://github.com/facebook/jest/pull/8764))
8+
79
### Chore & Maintenance
810

911
### Performance

packages/jest-fake-timers/src/__tests__/jestFakeTimers.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,5 +1303,22 @@ describe('FakeTimers', () => {
13031303

13041304
expect(timers.getTimerCount()).toEqual(3);
13051305
});
1306+
1307+
it('not includes cancelled immediates', () => {
1308+
const timers = new FakeTimers({
1309+
config,
1310+
global,
1311+
moduleMocker,
1312+
timerConfig,
1313+
});
1314+
1315+
timers.useFakeTimers();
1316+
1317+
global.setImmediate(() => {});
1318+
expect(timers.getTimerCount()).toEqual(1);
1319+
timers.clearAllTimers();
1320+
1321+
expect(timers.getTimerCount()).toEqual(0);
1322+
});
13061323
});
13071324
});

packages/jest-fake-timers/src/jestFakeTimers.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ const setGlobal = (
5353
};
5454

5555
export default class FakeTimers<TimerRef> {
56-
private _cancelledImmediates!: Record<string, boolean>;
5756
private _cancelledTicks!: Record<string, boolean>;
5857
private _config: StackTraceConfig;
5958
private _disposed?: boolean;
@@ -105,9 +104,7 @@ export default class FakeTimers<TimerRef> {
105104
}
106105

107106
clearAllTimers() {
108-
this._immediates.forEach(immediate =>
109-
this._fakeClearImmediate(immediate.uuid),
110-
);
107+
this._immediates = [];
111108
this._timers.clear();
112109
}
113110

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

119116
reset() {
120117
this._cancelledTicks = {};
121-
this._cancelledImmediates = {};
122118
this._now = 0;
123119
this._ticks = [];
124120
this._immediates = [];
@@ -177,10 +173,10 @@ export default class FakeTimers<TimerRef> {
177173
}
178174

179175
private _runImmediate(immediate: Tick) {
180-
if (!this._cancelledImmediates.hasOwnProperty(immediate.uuid)) {
181-
// Callback may throw, so update the map prior calling.
182-
this._cancelledImmediates[immediate.uuid] = true;
176+
try {
183177
immediate.callback();
178+
} finally {
179+
this._fakeClearImmediate(immediate.uuid);
184180
}
185181
}
186182

@@ -402,7 +398,9 @@ export default class FakeTimers<TimerRef> {
402398
}
403399

404400
private _fakeClearImmediate(uuid: TimerID) {
405-
this._cancelledImmediates[uuid] = true;
401+
this._immediates = this._immediates.filter(
402+
immediate => immediate.uuid !== uuid,
403+
);
406404
}
407405

408406
private _fakeNextTick(callback: Callback, ...args: Array<any>) {
@@ -432,19 +430,20 @@ export default class FakeTimers<TimerRef> {
432430
return null;
433431
}
434432

435-
const uuid = this._uuidCounter++;
433+
const uuid = String(this._uuidCounter++);
436434

437435
this._immediates.push({
438436
callback: () => callback.apply(null, args),
439-
uuid: String(uuid),
437+
uuid,
440438
});
441439

442-
const cancelledImmediates = this._cancelledImmediates;
443440
this._timerAPIs.setImmediate(() => {
444-
if (!cancelledImmediates.hasOwnProperty(uuid)) {
445-
// Callback may throw, so update the map prior calling.
446-
cancelledImmediates[String(uuid)] = true;
447-
callback.apply(null, args);
441+
if (this._immediates.find(x => x.uuid === uuid)) {
442+
try {
443+
callback.apply(null, args);
444+
} finally {
445+
this._fakeClearImmediate(uuid);
446+
}
448447
}
449448
});
450449

0 commit comments

Comments
 (0)