Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))

### Chore & Maintenance

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
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 @@ -486,7 +486,7 @@ export default class FakeTimers<TimerRef> {
return null;
}

if (delay == null) {
if (delay == null || Number.isNaN(delay) || !Number.isFinite(delay)) {
Copy link

Choose a reason for hiding this comment

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

You can cover all cases in one shot:

delay = Number(delay) | 0;

This invokes the same conversion algorithm as in the WebIDL spec. Number(x) coerces anything to a number (or NaN), and x | 0 truncates it to a 32-bit signed integer, with NaN and Infinity converted to 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tnx for the suggestion its indeed more straight forward and covers also more cases, updated 14881d7

delay = 0;
}

Expand Down