Skip to content
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

fix: remove side-effect from runWithRealTimers #887

Merged
merged 7 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
69 changes: 35 additions & 34 deletions src/__tests__/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import {
runWithRealTimers,
} from '../helpers'

const globalObj = typeof window === 'undefined' ? global : window

afterEach(() => jest.useRealTimers())

test('returns global document if exists', () => {
expect(getDocument()).toBe(document)
})
Expand Down Expand Up @@ -53,42 +49,47 @@ describe('query container validation throws when validation fails', () => {
})
})

test('should always use realTimers before using callback when timers are faked with useFakeTimers', () => {
const originalSetTimeout = globalObj.setTimeout
describe('run with real timers', () => {
const realSetTimeout = global.setTimeout

// legacy timers use mocks and do not rely on a clock instance
jest.useFakeTimers('legacy')
runWithRealTimers(() => {
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
afterEach(() => {
// restore timers replaced by jest.useFakeTimers()
jest.useRealTimers()
// restore setTimeout replaced by assignment
global.setTimeout = realSetTimeout
eps1lon marked this conversation as resolved.
Show resolved Hide resolved
})
expect(globalObj.setTimeout._isMockFunction).toBe(true)
expect(globalObj.setTimeout.clock).toBeUndefined()

jest.useRealTimers()

// modern timers use a clock instance instead of a mock
jest.useFakeTimers('modern')
runWithRealTimers(() => {
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
test('use real timers when timers are faked with jest.useFakeTimers(legacy)', () => {
// legacy timers use mocks and do not rely on a clock instance
jest.useFakeTimers('legacy')
runWithRealTimers(() => {
expect(global.setTimeout).toBe(realSetTimeout)
})
expect(global.setTimeout._isMockFunction).toBe(true)
expect(global.setTimeout.clock).toBeUndefined()
})
expect(globalObj.setTimeout._isMockFunction).toBeUndefined()
expect(globalObj.setTimeout.clock).toBeDefined()
})

test('should not use realTimers when timers are not faked with useFakeTimers', () => {
const originalSetTimeout = globalObj.setTimeout

// useFakeTimers is not used, timers are faked in some other way
const fakedSetTimeout = callback => {
callback()
}
fakedSetTimeout.clock = jest.fn()
test('use real timers when timers are faked with jest.useFakeTimers(modern)', () => {
// modern timers use a clock instance instead of a mock
jest.useFakeTimers('modern')
runWithRealTimers(() => {
expect(global.setTimeout).toBe(realSetTimeout)
})
expect(global.setTimeout._isMockFunction).toBeUndefined()
expect(global.setTimeout.clock).toBeDefined()
})

globalObj.setTimeout = fakedSetTimeout
test('do not use real timers when timers are not faked with jest.useFakeTimers', () => {
// useFakeTimers is not used, timers are faked in some other way
const fakedSetTimeout = callback => {
callback()
}
fakedSetTimeout.clock = jest.fn()
global.setTimeout = fakedSetTimeout

runWithRealTimers(() => {
expect(fakedSetTimeout).toEqual(globalObj.setTimeout)
runWithRealTimers(() => {
expect(global.setTimeout).toBe(fakedSetTimeout)
})
expect(global.setTimeout).toBe(fakedSetTimeout)
})

globalObj.setTimeout = originalSetTimeout
})
61 changes: 26 additions & 35 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,43 @@ const TEXT_NODE = 3

// Currently this fn only supports jest timers, but it could support other test runners in the future.
function runWithRealTimers(callback) {
const fakeTimersType = getJestFakeTimersType()
if (fakeTimersType) {
return _runWithRealTimers(callback).callbackReturnValue
}

function _runWithRealTimers(callback) {
const timerAPI = {
clearImmediate,
clearInterval,
clearTimeout,
setImmediate,
setInterval,
setTimeout,
}

try {
jest.useRealTimers()
} catch (e) {
// not running in jest environment
}

const callbackReturnValue = callback()

if (fakeTimersType) {
jest.useFakeTimers(fakeTimersType)
}
const usedJestFakeTimers = Object.keys(timerAPI).filter(
k => timerAPI[k] !== globalObj[k],
).length
ph-fritsche marked this conversation as resolved.
Show resolved Hide resolved

return callbackReturnValue
}

function getJestFakeTimersType() {
// istanbul ignore if
if (
typeof jest === 'undefined' ||
typeof globalObj.setTimeout === 'undefined'
) {
return null
if (usedJestFakeTimers) {
jest.useFakeTimers(timerAPI.setTimeout?.clock ? 'modern' : 'legacy')
}

if (
typeof globalObj.setTimeout._isMockFunction !== 'undefined' &&
globalObj.setTimeout._isMockFunction
) {
return 'legacy'
}

if (
typeof globalObj.setTimeout.clock !== 'undefined' &&
typeof jest.getRealSystemTime !== 'undefined'
) {
try {
// jest.getRealSystemTime is only supported for Jest's `modern` fake timers and otherwise throws
jest.getRealSystemTime()
return 'modern'
} catch {
// not using Jest's modern fake timers
}
return {
callbackReturnValue,
usedJestFakeTimers,
}
return null
}

const jestFakeTimersAreEnabled = () => Boolean(getJestFakeTimersType())
const jestFakeTimersAreEnabled = () =>
Boolean(_runWithRealTimers(() => {}).usedJestFakeTimers)

// we only run our tests in node, and setImmediate is supported in node.
// istanbul ignore next
Expand Down