Skip to content

Commit

Permalink
Fix runOnlyPendingTimers for setTimeout inside setImmediate (#4608)
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored and cpojer committed Oct 5, 2017
1 parent a85f379 commit 4f8f173
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/jest-util/src/__tests__/fake_timers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,19 +586,26 @@ 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',

'mock2',
'mock1',
'mock3',
'mock5',
]);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-util/src/fake_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4f8f173

Please sign in to comment.