Skip to content

fix: Guard against jest.useRealTimers not existing #934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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