Skip to content

Commit

Permalink
Fix handling of NaN/Infinity in mock timer delay, #5960 (#5966)
Browse files Browse the repository at this point in the history
* add handling for NaN as timer delay

* use Number.isNaN for isNaN checking

* handle Infinity values in timers delays correctly

* Fix handling of NaN/Infinity in mock timer delay

* use more universal delay validation
  • Loading branch information
eritikass authored and cpojer committed Apr 12, 2018
1 parent 36bfaf1 commit a7a65e2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
([#5914](https://github.com/facebook/jest/pull/5914))
* `[jest-regex-util]` Fix handling regex symbols in tests path on Windows
([#5941](https://github.com/facebook/jest/pull/5941))
* `[jest-util]` Fix handling of NaN/Infinity in mock timer delay
([#5966](https://github.com/facebook/jest/pull/5966))
* `[jest-resolve]` Generalise test for package main entries equivalent to ".".
([#5968)](https://github.com/facebook/jest/pull/5968)

Expand Down
15 changes: 13 additions & 2 deletions packages/jest-util/src/__tests__/fake_timers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,28 @@ describe('FakeTimers', () => {
const mock2 = jest.fn(() => runOrder.push('mock2'));
const mock3 = jest.fn(() => runOrder.push('mock3'));
const mock4 = jest.fn(() => runOrder.push('mock4'));
const mock5 = jest.fn(() => runOrder.push('mock5'));
const mock6 = jest.fn(() => runOrder.push('mock6'));

global.setTimeout(mock1, 100);
global.setTimeout(mock2, 0);
global.setTimeout(mock2, NaN);
global.setTimeout(mock3, 0);
const intervalHandler = global.setInterval(() => {
mock4();
global.clearInterval(intervalHandler);
}, 200);
global.setTimeout(mock5, Infinity);
global.setTimeout(mock6, -Infinity);

timers.runAllTimers();
expect(runOrder).toEqual(['mock2', 'mock3', 'mock1', 'mock4']);
expect(runOrder).toEqual([
'mock2',
'mock3',
'mock5',
'mock6',
'mock1',
'mock4',
]);
});

it('warns when trying to advance timers while real timers are used', () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/jest-util/src/fake_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,7 @@ export default class FakeTimers<TimerRef> {
return null;
}

if (delay == null) {
delay = 0;
}
delay = Number(delay) | 0;

const args = [];
for (let ii = 2, ll = arguments.length; ii < ll; ii++) {
Expand Down

0 comments on commit a7a65e2

Please sign in to comment.