From 9871003afd317ae82b2927c9a5d86ee3116cff2a Mon Sep 17 00:00:00 2001 From: Jonathan Conway Date: Mon, 22 Jun 2020 13:03:31 +1000 Subject: [PATCH] Add helpful error message to TimerMocks Help the reader to understand the context in which `jest.runOnlyPendingTimers()` is useful, by refering to the error that gets thrown when the timers are in an endless loop. --- docs/TimerMocks.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/TimerMocks.md b/docs/TimerMocks.md index 68c449847813..ebaa6e10328b 100644 --- a/docs/TimerMocks.md +++ b/docs/TimerMocks.md @@ -62,7 +62,15 @@ 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 // infiniteTimerGame.js