From bbd0d4abff2d721c13ced1eafa250eb5fec407e6 Mon Sep 17 00:00:00 2001 From: Siim Tiilen Date: Wed, 11 Apr 2018 23:13:33 +0300 Subject: [PATCH 1/5] add handling for NaN as timer delay --- CHANGELOG.md | 2 ++ packages/jest-util/src/__tests__/fake_timers.test.js | 2 +- packages/jest-util/src/fake_timers.js | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0fa49a26b8a..0899df09a3f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 for NaN as timer delay + ([#5966](https://github.com/facebook/jest/pull/5966)) ### Chore & Maintenance diff --git a/packages/jest-util/src/__tests__/fake_timers.test.js b/packages/jest-util/src/__tests__/fake_timers.test.js index b59e0e00be3f..e6a83c9055be 100644 --- a/packages/jest-util/src/__tests__/fake_timers.test.js +++ b/packages/jest-util/src/__tests__/fake_timers.test.js @@ -317,7 +317,7 @@ describe('FakeTimers', () => { const mock4 = jest.fn(() => runOrder.push('mock4')); global.setTimeout(mock1, 100); - global.setTimeout(mock2, 0); + global.setTimeout(mock2, NaN); global.setTimeout(mock3, 0); const intervalHandler = global.setInterval(() => { mock4(); diff --git a/packages/jest-util/src/fake_timers.js b/packages/jest-util/src/fake_timers.js index 0633a3fa0640..947e0ddbedd7 100644 --- a/packages/jest-util/src/fake_timers.js +++ b/packages/jest-util/src/fake_timers.js @@ -486,7 +486,7 @@ export default class FakeTimers { return null; } - if (delay == null) { + if (delay == null || isNaN(delay)) { delay = 0; } From 6d70f85acd1670af3b243f98f0caad4c62f6396a Mon Sep 17 00:00:00 2001 From: Siim Tiilen Date: Thu, 12 Apr 2018 00:51:17 +0300 Subject: [PATCH 2/5] use Number.isNaN for isNaN checking --- packages/jest-util/src/fake_timers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-util/src/fake_timers.js b/packages/jest-util/src/fake_timers.js index 947e0ddbedd7..4c44180bc7b1 100644 --- a/packages/jest-util/src/fake_timers.js +++ b/packages/jest-util/src/fake_timers.js @@ -486,7 +486,7 @@ export default class FakeTimers { return null; } - if (delay == null || isNaN(delay)) { + if (delay == null || Number.isNaN(delay)) { delay = 0; } From 3ef95bb958b261b65e3ee0934295532586d1e92a Mon Sep 17 00:00:00 2001 From: Siim Tiilen Date: Thu, 12 Apr 2018 00:57:07 +0300 Subject: [PATCH 3/5] handle Infinity values in timers delays correctly --- .../jest-util/src/__tests__/fake_timers.test.js | 13 ++++++++++++- packages/jest-util/src/fake_timers.js | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/jest-util/src/__tests__/fake_timers.test.js b/packages/jest-util/src/__tests__/fake_timers.test.js index e6a83c9055be..ac75ddf5ed01 100644 --- a/packages/jest-util/src/__tests__/fake_timers.test.js +++ b/packages/jest-util/src/__tests__/fake_timers.test.js @@ -315,6 +315,8 @@ 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, NaN); @@ -323,9 +325,18 @@ describe('FakeTimers', () => { 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', () => { diff --git a/packages/jest-util/src/fake_timers.js b/packages/jest-util/src/fake_timers.js index 4c44180bc7b1..8173f4cfbefd 100644 --- a/packages/jest-util/src/fake_timers.js +++ b/packages/jest-util/src/fake_timers.js @@ -486,7 +486,7 @@ export default class FakeTimers { return null; } - if (delay == null || Number.isNaN(delay)) { + if (delay == null || Number.isNaN(delay) || !Number.isFinite(delay)) { delay = 0; } From 62809a268a91c1943b3218686d9d167f52683957 Mon Sep 17 00:00:00 2001 From: Siim Tiilen Date: Thu, 12 Apr 2018 00:57:39 +0300 Subject: [PATCH 4/5] Fix handling of NaN/Infinity in mock timer delay --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0899df09a3f1..29d1f42083e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,7 +87,7 @@ ([#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 for NaN as timer delay +* `[jest-util]` Fix handling of NaN/Infinity in mock timer delay ([#5966](https://github.com/facebook/jest/pull/5966)) ### Chore & Maintenance From 14881d77e0056bd54265a3f9ffc2710c4fb58ac9 Mon Sep 17 00:00:00 2001 From: Siim Tiilen Date: Thu, 12 Apr 2018 01:47:15 +0300 Subject: [PATCH 5/5] use more universal delay validation --- packages/jest-util/src/fake_timers.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/jest-util/src/fake_timers.js b/packages/jest-util/src/fake_timers.js index 8173f4cfbefd..f0064e66f744 100644 --- a/packages/jest-util/src/fake_timers.js +++ b/packages/jest-util/src/fake_timers.js @@ -486,9 +486,7 @@ export default class FakeTimers { return null; } - if (delay == null || Number.isNaN(delay) || !Number.isFinite(delay)) { - delay = 0; - } + delay = Number(delay) | 0; const args = []; for (let ii = 2, ll = arguments.length; ii < ll; ii++) {