Skip to content

Commit

Permalink
fix: Guard against jest.useRealTimers not existing (#934)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
  • Loading branch information
ph-fritsche and eps1lon committed Apr 22, 2021
1 parent 793d598 commit 1b19094
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
typeof jest !== 'undefined' &&
jest !== null &&
typeof jest.useRealTimers === 'function'
)
}

function runWithJestRealTimers(callback) {
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1b19094

Please sign in to comment.