@@ -5,10 +5,6 @@ import {
5
5
runWithRealTimers ,
6
6
} from '../helpers'
7
7
8
- const globalObj = typeof window === 'undefined' ? global : window
9
-
10
- afterEach ( ( ) => jest . useRealTimers ( ) )
11
-
12
8
test ( 'returns global document if exists' , ( ) => {
13
9
expect ( getDocument ( ) ) . toBe ( document )
14
10
} )
@@ -53,42 +49,47 @@ describe('query container validation throws when validation fails', () => {
53
49
} )
54
50
} )
55
51
56
- test ( 'should always use realTimers before using callback when timers are faked with useFakeTimers ', ( ) => {
57
- const originalSetTimeout = globalObj . setTimeout
52
+ describe ( 'run with real timers', ( ) => {
53
+ const realSetTimeout = global . setTimeout
58
54
59
- // legacy timers use mocks and do not rely on a clock instance
60
- jest . useFakeTimers ( 'legacy' )
61
- runWithRealTimers ( ( ) => {
62
- expect ( originalSetTimeout ) . toEqual ( globalObj . setTimeout )
55
+ afterEach ( ( ) => {
56
+ // restore timers replaced by jest.useFakeTimers()
57
+ jest . useRealTimers ( )
58
+ // restore setTimeout replaced by assignment
59
+ global . setTimeout = realSetTimeout
63
60
} )
64
- expect ( globalObj . setTimeout . _isMockFunction ) . toBe ( true )
65
- expect ( globalObj . setTimeout . clock ) . toBeUndefined ( )
66
61
67
- jest . useRealTimers ( )
68
-
69
- // modern timers use a clock instance instead of a mock
70
- jest . useFakeTimers ( 'modern' )
71
- runWithRealTimers ( ( ) => {
72
- expect ( originalSetTimeout ) . toEqual ( globalObj . setTimeout )
62
+ test ( 'use real timers when timers are faked with jest.useFakeTimers(legacy)' , ( ) => {
63
+ // legacy timers use mocks and do not rely on a clock instance
64
+ jest . useFakeTimers ( 'legacy' )
65
+ runWithRealTimers ( ( ) => {
66
+ expect ( global . setTimeout ) . toBe ( realSetTimeout )
67
+ } )
68
+ expect ( global . setTimeout . _isMockFunction ) . toBe ( true )
69
+ expect ( global . setTimeout . clock ) . toBeUndefined ( )
73
70
} )
74
- expect ( globalObj . setTimeout . _isMockFunction ) . toBeUndefined ( )
75
- expect ( globalObj . setTimeout . clock ) . toBeDefined ( )
76
- } )
77
71
78
- test ( 'should not use realTimers when timers are not faked with useFakeTimers' , ( ) => {
79
- const originalSetTimeout = globalObj . setTimeout
80
-
81
- // useFakeTimers is not used, timers are faked in some other way
82
- const fakedSetTimeout = callback => {
83
- callback ( )
84
- }
85
- fakedSetTimeout . clock = jest . fn ( )
72
+ test ( 'use real timers when timers are faked with jest.useFakeTimers(modern)' , ( ) => {
73
+ // modern timers use a clock instance instead of a mock
74
+ jest . useFakeTimers ( 'modern' )
75
+ runWithRealTimers ( ( ) => {
76
+ expect ( global . setTimeout ) . toBe ( realSetTimeout )
77
+ } )
78
+ expect ( global . setTimeout . _isMockFunction ) . toBeUndefined ( )
79
+ expect ( global . setTimeout . clock ) . toBeDefined ( )
80
+ } )
86
81
87
- globalObj . setTimeout = fakedSetTimeout
82
+ test ( 'do not use real timers when timers are not faked with jest.useFakeTimers' , ( ) => {
83
+ // useFakeTimers is not used, timers are faked in some other way
84
+ const fakedSetTimeout = callback => {
85
+ callback ( )
86
+ }
87
+ fakedSetTimeout . clock = jest . fn ( )
88
+ global . setTimeout = fakedSetTimeout
88
89
89
- runWithRealTimers ( ( ) => {
90
- expect ( fakedSetTimeout ) . toEqual ( globalObj . setTimeout )
90
+ runWithRealTimers ( ( ) => {
91
+ expect ( global . setTimeout ) . toBe ( fakedSetTimeout )
92
+ } )
93
+ expect ( global . setTimeout ) . toBe ( fakedSetTimeout )
91
94
} )
92
-
93
- globalObj . setTimeout = originalSetTimeout
94
95
} )
0 commit comments