From 87d2195120cbfcd30971aeb5728376b460330fcb Mon Sep 17 00:00:00 2001 From: Philipp Fritsche Date: Thu, 22 Apr 2021 11:20:41 +0200 Subject: [PATCH 1/2] fix: check if `jest.useRealTimers` exists --- src/helpers.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 559e39d2..699b3460 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -5,13 +5,18 @@ const TEXT_NODE = 3 // Currently this fn only supports jest timers, but it could support other test runners in the future. function runWithRealTimers(callback) { - // istanbul ignore else - if (typeof jest !== 'undefined') { - return runWithJestRealTimers(callback).callbackReturnValue - } + return hasJestTimers() + ? runWithJestRealTimers(callback).callbackReturnValue + : // istanbul ignore next + callback() +} - // istanbul ignore next - return callback() +function hasJestTimers() { + return ( + jest !== undefined && + jest !== null && + typeof jest.useRealTimers === 'function' + ) } function runWithJestRealTimers(callback) { @@ -50,13 +55,10 @@ function runWithJestRealTimers(callback) { } function jestFakeTimersAreEnabled() { - // istanbul ignore else - if (typeof jest !== 'undefined') { - return runWithJestRealTimers(() => {}).usedFakeTimers - } - - // istanbul ignore next - return false + return hasJestTimers() + ? runWithJestRealTimers(() => {}).usedFakeTimers + : // istanbul ignore next + false } // we only run our tests in node, and setImmediate is supported in node. From 80a6af82d298f8d039670645b9a627dae8655664 Mon Sep 17 00:00:00 2001 From: Philipp Fritsche Date: Thu, 22 Apr 2021 12:02:19 +0200 Subject: [PATCH 2/2] Update src/helpers.js Co-authored-by: Sebastian Silbermann --- src/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.js b/src/helpers.js index 699b3460..4536ddd5 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -13,7 +13,7 @@ function runWithRealTimers(callback) { function hasJestTimers() { return ( - jest !== undefined && + typeof jest !== 'undefined' && jest !== null && typeof jest.useRealTimers === 'function' )