From 9d17ea9297ab6e4c697899a99ee35ae97a4a1c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Mon, 26 Jun 2017 22:58:18 +0200 Subject: [PATCH] Make sure runAllTimers also clears all ticks --- .../jest-util/src/__tests__/fake_timers.test.js | 15 +++++++++++++++ packages/jest-util/src/fake_timers.js | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/packages/jest-util/src/__tests__/fake_timers.test.js b/packages/jest-util/src/__tests__/fake_timers.test.js index 210dfac4e556..41a8e4e5dc1f 100644 --- a/packages/jest-util/src/__tests__/fake_timers.test.js +++ b/packages/jest-util/src/__tests__/fake_timers.test.js @@ -406,6 +406,21 @@ describe('FakeTimers', () => { ), ); }); + + it('also clears ticks', () => { + const global = {process}; + const timers = new FakeTimers(global, moduleMocker); + timers.useFakeTimers(); + + const fn = jest.genMockFn(); + global.setTimeout(() => { + process.nextTick(fn); + }, 0); + expect(fn.mock.calls.length).toBe(0); + + timers.runAllTimers(); + expect(fn.mock.calls.length).toBe(1); + }); }); describe('runTimersToTime', () => { diff --git a/packages/jest-util/src/fake_timers.js b/packages/jest-util/src/fake_timers.js index b0d47a59daa6..001a66455cf2 100644 --- a/packages/jest-util/src/fake_timers.js +++ b/packages/jest-util/src/fake_timers.js @@ -219,6 +219,10 @@ class FakeTimers { if (this._immediates.length) { this.runAllImmediates(); } + + if (this._ticks.length) { + this.runAllTicks(); + } } if (i === this._maxLoops) {