From 3e95e31e82df14cadc8b683bca6043069ed7af45 Mon Sep 17 00:00:00 2001 From: Biki-das Date: Sun, 16 Jan 2022 10:54:51 +0530 Subject: [PATCH 1/3] Added an error message to Timermocks --- docs/TimerMocks.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/TimerMocks.md b/docs/TimerMocks.md index b053c1dbdfc9..3a17c3a971aa 100644 --- a/docs/TimerMocks.md +++ b/docs/TimerMocks.md @@ -78,7 +78,13 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop… so something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: + +``` +Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... +``` + +So something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: ```javascript title="infiniteTimerGame.js" 'use strict'; From 6f67bd2cbe591c9b626c6319dfd1932b7d40d4d2 Mon Sep 17 00:00:00 2001 From: Biki-das Date: Sun, 16 Jan 2022 11:15:08 +0530 Subject: [PATCH 2/3] Changes applied to other versioned docs --- website/versioned_docs/version-25.x/TimerMocks.md | 8 +++++++- website/versioned_docs/version-26.x/TimerMocks.md | 8 +++++++- website/versioned_docs/version-27.0/TimerMocks.md | 8 +++++++- website/versioned_docs/version-27.1/TimerMocks.md | 8 +++++++- website/versioned_docs/version-27.2/TimerMocks.md | 15 +++++++++++++-- website/versioned_docs/version-27.4/TimerMocks.md | 8 +++++++- 6 files changed, 48 insertions(+), 7 deletions(-) diff --git a/website/versioned_docs/version-25.x/TimerMocks.md b/website/versioned_docs/version-25.x/TimerMocks.md index 8086ccd10b8b..26615ff64e20 100644 --- a/website/versioned_docs/version-25.x/TimerMocks.md +++ b/website/versioned_docs/version-25.x/TimerMocks.md @@ -60,7 +60,13 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop… so something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop ,throwing the following error: + +``` +Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... +``` + +So something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: ```javascript title="infiniteTimerGame.js" 'use strict'; diff --git a/website/versioned_docs/version-26.x/TimerMocks.md b/website/versioned_docs/version-26.x/TimerMocks.md index 8086ccd10b8b..82c34f4ef22d 100644 --- a/website/versioned_docs/version-26.x/TimerMocks.md +++ b/website/versioned_docs/version-26.x/TimerMocks.md @@ -60,7 +60,13 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop… so something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: + +``` +Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... +``` + +So something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: ```javascript title="infiniteTimerGame.js" 'use strict'; diff --git a/website/versioned_docs/version-27.0/TimerMocks.md b/website/versioned_docs/version-27.0/TimerMocks.md index b053c1dbdfc9..3a17c3a971aa 100644 --- a/website/versioned_docs/version-27.0/TimerMocks.md +++ b/website/versioned_docs/version-27.0/TimerMocks.md @@ -78,7 +78,13 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop… so something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: + +``` +Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... +``` + +So something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: ```javascript title="infiniteTimerGame.js" 'use strict'; diff --git a/website/versioned_docs/version-27.1/TimerMocks.md b/website/versioned_docs/version-27.1/TimerMocks.md index b053c1dbdfc9..3a17c3a971aa 100644 --- a/website/versioned_docs/version-27.1/TimerMocks.md +++ b/website/versioned_docs/version-27.1/TimerMocks.md @@ -78,7 +78,13 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop… so something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: + +``` +Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... +``` + +So something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: ```javascript title="infiniteTimerGame.js" 'use strict'; diff --git a/website/versioned_docs/version-27.2/TimerMocks.md b/website/versioned_docs/version-27.2/TimerMocks.md index b053c1dbdfc9..a8e114d82229 100644 --- a/website/versioned_docs/version-27.2/TimerMocks.md +++ b/website/versioned_docs/version-27.2/TimerMocks.md @@ -22,7 +22,7 @@ module.exports = timerGame; ```javascript title="__tests__/timerGame-test.js" 'use strict'; -jest.useFakeTimers(); +jest.useFakeTimers(); // or you can set "timers": "fake" globally in configuration file jest.spyOn(global, 'setTimeout'); test('waits 1 second before ending the game', () => { @@ -53,11 +53,14 @@ test('do something with real timers', () => { }); ``` +All of the following functions need fake timers to be set, either by `jest.useFakeTimers()` or via `"timers": "fake"` in the config file. + ## Run All Timers Another test we might want to write for this module is one that asserts that the callback is called after 1 second. To do this, we're going to use Jest's timer control APIs to fast-forward time right in the middle of the test: ```javascript +jest.useFakeTimers(); test('calls the callback after 1 second', () => { const timerGame = require('../timerGame'); const callback = jest.fn(); @@ -78,7 +81,13 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop… so something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: + +``` +Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... +``` + +So something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: ```javascript title="infiniteTimerGame.js" 'use strict'; @@ -151,6 +160,8 @@ module.exports = timerGame; ``` ```javascript +// __tests__/timerGame-test.js +jest.useFakeTimers(); it('calls the callback after 1 second via advanceTimersByTime', () => { const timerGame = require('../timerGame'); const callback = jest.fn(); diff --git a/website/versioned_docs/version-27.4/TimerMocks.md b/website/versioned_docs/version-27.4/TimerMocks.md index b053c1dbdfc9..3a17c3a971aa 100644 --- a/website/versioned_docs/version-27.4/TimerMocks.md +++ b/website/versioned_docs/version-27.4/TimerMocks.md @@ -78,7 +78,13 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop… so something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: + +``` +Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... +``` + +So something like `jest.runAllTimers()` is not desirable. For these cases you might use `jest.runOnlyPendingTimers()`: ```javascript title="infiniteTimerGame.js" 'use strict'; From 188e3a847609b597a455318e59efd75e3b11265d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 5 Feb 2022 08:41:02 +0100 Subject: [PATCH 3/3] Apply suggestions from code review --- docs/TimerMocks.md | 2 +- website/versioned_docs/version-25.x/TimerMocks.md | 2 +- website/versioned_docs/version-26.x/TimerMocks.md | 2 +- website/versioned_docs/version-27.0/TimerMocks.md | 2 +- website/versioned_docs/version-27.1/TimerMocks.md | 2 +- website/versioned_docs/version-27.2/TimerMocks.md | 2 +- website/versioned_docs/version-27.4/TimerMocks.md | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/TimerMocks.md b/docs/TimerMocks.md index 94e2fa18d012..29613b26dbff 100644 --- a/docs/TimerMocks.md +++ b/docs/TimerMocks.md @@ -79,7 +79,7 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop, throwing the following error: ``` Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... diff --git a/website/versioned_docs/version-25.x/TimerMocks.md b/website/versioned_docs/version-25.x/TimerMocks.md index a8d1f2c3975d..8598c63c7e6c 100644 --- a/website/versioned_docs/version-25.x/TimerMocks.md +++ b/website/versioned_docs/version-25.x/TimerMocks.md @@ -63,7 +63,7 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop ,throwing the following error: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop, throwing the following error: ``` Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... diff --git a/website/versioned_docs/version-26.x/TimerMocks.md b/website/versioned_docs/version-26.x/TimerMocks.md index 47ba29618da1..8598c63c7e6c 100644 --- a/website/versioned_docs/version-26.x/TimerMocks.md +++ b/website/versioned_docs/version-26.x/TimerMocks.md @@ -63,7 +63,7 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop, throwing the following error: ``` Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... diff --git a/website/versioned_docs/version-27.0/TimerMocks.md b/website/versioned_docs/version-27.0/TimerMocks.md index 4581312bdaa8..44c210c4eebe 100644 --- a/website/versioned_docs/version-27.0/TimerMocks.md +++ b/website/versioned_docs/version-27.0/TimerMocks.md @@ -81,7 +81,7 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop, throwing the following error: ``` Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... diff --git a/website/versioned_docs/version-27.1/TimerMocks.md b/website/versioned_docs/version-27.1/TimerMocks.md index 4581312bdaa8..44c210c4eebe 100644 --- a/website/versioned_docs/version-27.1/TimerMocks.md +++ b/website/versioned_docs/version-27.1/TimerMocks.md @@ -81,7 +81,7 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop, throwing the following error: ``` Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... diff --git a/website/versioned_docs/version-27.2/TimerMocks.md b/website/versioned_docs/version-27.2/TimerMocks.md index 4581312bdaa8..44c210c4eebe 100644 --- a/website/versioned_docs/version-27.2/TimerMocks.md +++ b/website/versioned_docs/version-27.2/TimerMocks.md @@ -81,7 +81,7 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop, throwing the following error: ``` Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out... diff --git a/website/versioned_docs/version-27.4/TimerMocks.md b/website/versioned_docs/version-27.4/TimerMocks.md index 4581312bdaa8..44c210c4eebe 100644 --- a/website/versioned_docs/version-27.4/TimerMocks.md +++ b/website/versioned_docs/version-27.4/TimerMocks.md @@ -81,7 +81,7 @@ test('calls the callback after 1 second', () => { ## Run Pending Timers -There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop,throwing the following error: +There are also scenarios where you might have a recursive timer -- that is a timer that sets a new timer in its own callback. For these, running all the timers would be an endless loop, throwing the following error: ``` Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out...