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
Prev Previous commit
Next Next commit
fix snapshot test fail on incorrect error
eranshabi committed Aug 15, 2019
commit 9880b246fad64c7ee4999634ad42302b8fddfde2
12 changes: 9 additions & 3 deletions packages/jest-fake-timers/src/jestFakeTimers.ts
Original file line number Diff line number Diff line change
@@ -173,8 +173,8 @@ export default class FakeTimers<TimerRef> {
}

private _runImmediate(immediate: Tick) {
this._fakeClearImmediate(immediate.uuid);
immediate.callback();
this._fakeClearImmediate(immediate.uuid);
}

runAllTimers() {
@@ -436,8 +436,14 @@ export default class FakeTimers<TimerRef> {

const immediates = this._immediates;
this._timerAPIs.setImmediate(() => {
this._fakeClearImmediate(uuid);
immediates.find(x => x.uuid === uuid) && callback.apply(null, args);
if (immediates.find(x => x.uuid === uuid)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this if?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please refer to this comment: #8764 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, why do we have find at all?

Copy link
Contributor Author

@eranshabi eranshabi Aug 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we don't run immediates more than once.
If it was cleared (not in the immediates array) then it shouldn't run even if runAllImmediates is called

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, makes sense

try {
callback.apply(null, args);
} catch (error) {
} finally {
this._fakeClearImmediate(uuid);
}
}
});

return uuid;