diff --git a/packages/jest-util/src/__tests__/fake_timers.test.js b/packages/jest-util/src/__tests__/fake_timers.test.js index a0892824784c..ff2a2afc9596 100644 --- a/packages/jest-util/src/__tests__/fake_timers.test.js +++ b/packages/jest-util/src/__tests__/fake_timers.test.js @@ -586,12 +586,18 @@ describe('FakeTimers', () => { runOrder.push('mock4'); }); + global.setImmediate(function cb() { + runOrder.push('mock5'); + global.setTimeout(cb, 400); + }); + timers.runOnlyPendingTimers(); - expect(runOrder).toEqual(['mock4', 'mock2', 'mock1', 'mock3']); + expect(runOrder).toEqual(['mock4', 'mock5', 'mock2', 'mock1', 'mock3']); timers.runOnlyPendingTimers(); expect(runOrder).toEqual([ 'mock4', + 'mock5', 'mock2', 'mock1', 'mock3', @@ -599,6 +605,7 @@ describe('FakeTimers', () => { 'mock2', 'mock1', 'mock3', + 'mock5', ]); }); diff --git a/packages/jest-util/src/fake_timers.js b/packages/jest-util/src/fake_timers.js index 6881053a5331..6323ad19adf1 100644 --- a/packages/jest-util/src/fake_timers.js +++ b/packages/jest-util/src/fake_timers.js @@ -235,9 +235,9 @@ export default class FakeTimers { } runOnlyPendingTimers() { + const timers = Object.assign({}, this._timers); this._checkFakeTimers(); this._immediates.forEach(this._runImmediate, this); - const timers = this._timers; Object.keys(timers) .sort((left, right) => timers[left].expiry - timers[right].expiry) .forEach(this._runTimerHandle, this);