Skip to content
Merged
Show file tree
Hide file tree
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
27 changes: 26 additions & 1 deletion src/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import {getDocument, getWindowFromNode, checkContainerType} from '../helpers'
import {
getDocument,
getWindowFromNode,
checkContainerType,
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 @@ -43,3 +52,19 @@ describe('query container validation throws when validation fails', () => {
)
})
})

test('should always use realTimers before using callback', () => {
const originalSetTimeout = globalObj.setTimeout

jest.useFakeTimers('legacy')
runWithRealTimers(() => {
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
})

jest.useRealTimers()

jest.useFakeTimers('modern')
runWithRealTimers(() => {
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
})
})
3 changes: 2 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const globalObj = typeof window === 'undefined' ? global : window
function runWithRealTimers(callback) {
const usingJestFakeTimers =
globalObj.setTimeout &&
globalObj.setTimeout._isMockFunction &&
(globalObj.setTimeout._isMockFunction ||
typeof globalObj.setTimeout.clock !== 'undefined') &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sensible 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to find a different way but I didn't find

typeof jest !== 'undefined'

if (usingJestFakeTimers) {
Expand Down