From 3108e5b22d542f54f6beaad319451c0be776b443 Mon Sep 17 00:00:00 2001 From: eps1lon Date: Fri, 19 Feb 2021 19:29:13 +0100 Subject: [PATCH 1/4] fix: Don't assume micked timers imply jest fake timers --- src/helpers.js | 6 +++--- src/wait-for.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 7b8d95e5..6a4254d7 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -25,9 +25,9 @@ function _runWithRealTimers(callback) { const callbackReturnValue = callback() - const usedJestFakeTimers = Object.entries(timerAPI).some( - ([name, func]) => func !== globalObj[name], - ) + const usedJestFakeTimers = + typeof jest !== 'undefined' && + Object.entries(timerAPI).some(([name, func]) => func !== globalObj[name]) if (usedJestFakeTimers) { jest.useFakeTimers(timerAPI.setTimeout?.clock ? 'modern' : 'legacy') diff --git a/src/wait-for.js b/src/wait-for.js index 9eb44842..860b7a15 100644 --- a/src/wait-for.js +++ b/src/wait-for.js @@ -52,8 +52,8 @@ function waitFor( const overallTimeoutTimer = setTimeout(handleTimeout, timeout) - const usingFakeTimers = jestFakeTimersAreEnabled() - if (usingFakeTimers) { + const usingJestFakeTimers = jestFakeTimersAreEnabled() + if (usingJestFakeTimers) { checkCallback() // this is a dangerous rule to disable because it could lead to an // infinite loop. However, eslint isn't smart enough to know that we're @@ -107,7 +107,7 @@ function waitFor( finished = true clearTimeout(overallTimeoutTimer) - if (!usingFakeTimers) { + if (!usingJestFakeTimers) { clearInterval(intervalId) observer.disconnect() } From a29bd919b3815912f76a1cb95200d43c8292f154 Mon Sep 17 00:00:00 2001 From: eps1lon Date: Fri, 19 Feb 2021 19:44:02 +0100 Subject: [PATCH 2/4] Create dedicated jest timer functions The naming should indicate that they should only be called in a jest-like environment --- src/helpers.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 6a4254d7..96ecbc04 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -5,10 +5,15 @@ const TEXT_NODE = 3 // Currently this fn only supports jest timers, but it could support other test runners in the future. function runWithRealTimers(callback) { - return _runWithRealTimers(callback).callbackReturnValue + // istanbul ignore else + if (typeof jest !== 'undefined') { + return runWithJestRealTimers(callback).callbackReturnValue + } + + return callback() } -function _runWithRealTimers(callback) { +function runWithJestRealTimers(callback) { const timerAPI = { clearImmediate, clearInterval, @@ -18,29 +23,26 @@ function _runWithRealTimers(callback) { setTimeout, } - // istanbul ignore else - if (typeof jest !== 'undefined') { - jest.useRealTimers() - } + jest.useRealTimers() const callbackReturnValue = callback() - const usedJestFakeTimers = - typeof jest !== 'undefined' && - Object.entries(timerAPI).some(([name, func]) => func !== globalObj[name]) + const usedFakeTimers = Object.entries(timerAPI).some( + ([name, func]) => func !== globalObj[name], + ) - if (usedJestFakeTimers) { + if (usedFakeTimers) { jest.useFakeTimers(timerAPI.setTimeout?.clock ? 'modern' : 'legacy') } return { callbackReturnValue, - usedJestFakeTimers, + usedFakeTimers, } } const jestFakeTimersAreEnabled = () => - Boolean(_runWithRealTimers(() => {}).usedJestFakeTimers) + typeof jest !== 'undefined' && runWithJestRealTimers(() => {}).usedFakeTimers // we only run our tests in node, and setImmediate is supported in node. // istanbul ignore next From bb44f291e22bb8148ad412df708ce00c8a734330 Mon Sep 17 00:00:00 2001 From: eps1lon Date: Fri, 19 Feb 2021 19:51:22 +0100 Subject: [PATCH 3/4] No implicit return --- src/helpers.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 96ecbc04..5b77e231 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -41,8 +41,13 @@ function runWithJestRealTimers(callback) { } } -const jestFakeTimersAreEnabled = () => - typeof jest !== 'undefined' && runWithJestRealTimers(() => {}).usedFakeTimers +function jestFakeTimersAreEnabled() { + // istanbul ignore else + if (typeof jest !== 'undefined') { + return runWithJestRealTimers(() => {}).usedFakeTimers + } + return false +} // we only run our tests in node, and setImmediate is supported in node. // istanbul ignore next From 8867abe124e15b0ae15faf4551b6bb19475bc7db Mon Sep 17 00:00:00 2001 From: eps1lon Date: Fri, 19 Feb 2021 19:54:55 +0100 Subject: [PATCH 4/4] I don't know how istanbul works and I don't care --- src/helpers.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/helpers.js b/src/helpers.js index 5b77e231..176b606f 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -10,6 +10,7 @@ function runWithRealTimers(callback) { return runWithJestRealTimers(callback).callbackReturnValue } + // istanbul ignore next return callback() } @@ -46,6 +47,8 @@ function jestFakeTimersAreEnabled() { if (typeof jest !== 'undefined') { return runWithJestRealTimers(() => {}).usedFakeTimers } + + // istanbul ignore next return false }