@@ -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,107 +49,44 @@ describe('query container validation throws when validation fails', () => {
53
49
} )
54
50
} )
55
51
56
- describe ( 'analyze test leak ' , ( ) => {
52
+ describe ( 'run with real timers ' , ( ) => {
57
53
const realSetTimeout = global . setTimeout
58
54
59
55
afterEach ( ( ) => {
56
+ jest . useRealTimers ( )
60
57
global . setTimeout = realSetTimeout
61
58
} )
62
59
63
- test ( 'jest.getRealSystemTime does not throw after modern timers have been used' , ( ) => {
64
- expect ( global . setTimeout ) . toBe ( realSetTimeout )
65
-
66
- expect ( jest . getRealSystemTime ) . toThrow ( )
67
-
68
- jest . useRealTimers ( )
69
- expect ( global . setTimeout ) . toBe ( realSetTimeout )
70
-
71
- jest . useFakeTimers ( 'modern' )
72
- expect ( jest . getRealSystemTime ) . not . toThrow ( )
73
-
74
- jest . useRealTimers ( )
75
- expect ( global . setTimeout ) . toBe ( realSetTimeout )
76
-
77
- // current implementation assumes this throws
78
- // see https://github.com/testing-library/dom-testing-library/blob/5bc93643f312d9ca4210b97681686c9aa8a902d7/src/helpers.js#L43-L45
79
- expect ( jest . getRealSystemTime ) . not . toThrow ( )
80
-
81
- // jest.useRealTimers() is still no problem
82
- jest . useRealTimers ( )
83
- expect ( global . setTimeout ) . toBe ( realSetTimeout )
60
+ test ( 'use real timers when timers are faked with jest.useFakeTimers(legacy)' , ( ) => {
61
+ // legacy timers use mocks and do not rely on a clock instance
62
+ jest . useFakeTimers ( 'legacy' )
63
+ runWithRealTimers ( ( ) => {
64
+ expect ( global . setTimeout ) . toEqual ( realSetTimeout )
65
+ } )
66
+ expect ( global . setTimeout . _isMockFunction ) . toBe ( true )
67
+ expect ( global . setTimeout . clock ) . toBeUndefined ( )
84
68
} )
85
69
86
- test ( 'BUG: runWithRealTimers fakes timers after modern fake timers have been used' , ( ) => {
70
+ test ( 'use real timers when timers are faked with jest.useFakeTimers(modern)' , ( ) => {
71
+ // modern timers use a clock instance instead of a mock
87
72
jest . useFakeTimers ( 'modern' )
88
- jest . useRealTimers ( )
89
-
90
- // modern fake timers have been used, but the current timeout is the real one
91
- expect ( global . setTimeout ) . toBe ( realSetTimeout )
73
+ runWithRealTimers ( ( ) => {
74
+ expect ( global . setTimeout ) . toEqual ( realSetTimeout )
75
+ } )
76
+ expect ( global . setTimeout . _isMockFunction ) . toBeUndefined ( )
77
+ expect ( global . setTimeout . clock ) . toBeDefined ( )
78
+ } )
92
79
93
- // repeat the test for the fake implementation
80
+ test ( 'do not use real timers when timers are not faked with jest.useFakeTimers' , ( ) => {
81
+ // useFakeTimers is not used, timers are faked in some other way
94
82
const fakedSetTimeout = callback => {
95
83
callback ( )
96
84
}
97
85
fakedSetTimeout . clock = jest . fn ( )
98
86
global . setTimeout = fakedSetTimeout
99
87
100
- // runWithRealTimers assumes jest.getRealSystemTime() would throw, but it does not
101
- // it calls jest.useRealTimers() before the callback - which does nothing because no fake is active
102
- // it calls jest.useFakeTimers('modern') after the callback
103
88
runWithRealTimers ( ( ) => {
104
- expect ( fakedSetTimeout ) . toEqual ( globalObj . setTimeout )
89
+ expect ( global . setTimeout ) . toEqual ( fakedSetTimeout )
105
90
} )
106
-
107
- // it should be fakedSetTimeout
108
- expect ( global . setTimeout ) . not . toBe ( fakedSetTimeout )
109
-
110
- // this line makes the bug hard to track down
111
- // without it jest.useRealTimers() will restore fakedSetTimeout
112
- // with it jest.useRealTimers() 'restores' setTimeout to undefined
113
- global . setTimeout = realSetTimeout
114
-
115
- // now useRealTimers is broken
116
- jest . useRealTimers ( )
117
- expect ( global . setTimeout ) . not . toBe ( realSetTimeout )
118
91
} )
119
92
} )
120
-
121
- test ( 'should always use realTimers before using callback when timers are faked with useFakeTimers' , ( ) => {
122
- const originalSetTimeout = globalObj . setTimeout
123
-
124
- // legacy timers use mocks and do not rely on a clock instance
125
- jest . useFakeTimers ( 'legacy' )
126
- runWithRealTimers ( ( ) => {
127
- expect ( originalSetTimeout ) . toEqual ( globalObj . setTimeout )
128
- } )
129
- expect ( globalObj . setTimeout . _isMockFunction ) . toBe ( true )
130
- expect ( globalObj . setTimeout . clock ) . toBeUndefined ( )
131
-
132
- jest . useRealTimers ( )
133
-
134
- // modern timers use a clock instance instead of a mock
135
- jest . useFakeTimers ( 'modern' )
136
- runWithRealTimers ( ( ) => {
137
- expect ( originalSetTimeout ) . toEqual ( globalObj . setTimeout )
138
- } )
139
- expect ( globalObj . setTimeout . _isMockFunction ) . toBeUndefined ( )
140
- expect ( globalObj . setTimeout . clock ) . toBeDefined ( )
141
- } )
142
-
143
- test ( 'should not use realTimers when timers are not faked with useFakeTimers' , ( ) => {
144
- const originalSetTimeout = globalObj . setTimeout
145
-
146
- // useFakeTimers is not used, timers are faked in some other way
147
- const fakedSetTimeout = callback => {
148
- callback ( )
149
- }
150
- fakedSetTimeout . clock = jest . fn ( )
151
-
152
- globalObj . setTimeout = fakedSetTimeout
153
-
154
- runWithRealTimers ( ( ) => {
155
- expect ( fakedSetTimeout ) . toEqual ( globalObj . setTimeout )
156
- } )
157
-
158
- globalObj . setTimeout = originalSetTimeout
159
- } )
0 commit comments