Skip to content

Commit 167b4ac

Browse files
authored
fix: runWithRealTimers to be compatible with new version of jest (#649)
Closes #612
1 parent 446cce8 commit 167b4ac

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/__tests__/helpers.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import {getDocument, getWindowFromNode, checkContainerType} from '../helpers'
1+
import {
2+
getDocument,
3+
getWindowFromNode,
4+
checkContainerType,
5+
runWithRealTimers,
6+
} from '../helpers'
7+
8+
const globalObj = typeof window === 'undefined' ? global : window
9+
10+
afterEach(() => jest.useRealTimers())
211

312
test('returns global document if exists', () => {
413
expect(getDocument()).toBe(document)
@@ -43,3 +52,19 @@ describe('query container validation throws when validation fails', () => {
4352
)
4453
})
4554
})
55+
56+
test('should always use realTimers before using callback', () => {
57+
const originalSetTimeout = globalObj.setTimeout
58+
59+
jest.useFakeTimers('legacy')
60+
runWithRealTimers(() => {
61+
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
62+
})
63+
64+
jest.useRealTimers()
65+
66+
jest.useFakeTimers('modern')
67+
runWithRealTimers(() => {
68+
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
69+
})
70+
})

src/helpers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const globalObj = typeof window === 'undefined' ? global : window
44
function runWithRealTimers(callback) {
55
const usingJestFakeTimers =
66
globalObj.setTimeout &&
7-
globalObj.setTimeout._isMockFunction &&
7+
(globalObj.setTimeout._isMockFunction ||
8+
typeof globalObj.setTimeout.clock !== 'undefined') &&
89
typeof jest !== 'undefined'
910

1011
if (usingJestFakeTimers) {

0 commit comments

Comments
 (0)