@@ -11,6 +11,10 @@ async function runWaitFor({time = 300} = {}, options) {
11
11
await waitFor ( ( ) => expect ( result ) . toBe ( response ) , options )
12
12
}
13
13
14
+ afterEach ( ( ) => {
15
+ jest . useRealTimers ( )
16
+ } )
17
+
14
18
test ( 'real timers' , async ( ) => {
15
19
// the only difference when not using fake timers is this test will
16
20
// have to wait the full length of the timeout
@@ -40,9 +44,10 @@ test('fake timer timeout', async () => {
40
44
} )
41
45
42
46
test ( 'times out after 1000ms by default' , async ( ) => {
47
+ const startReal = performance . now ( )
43
48
jest . useFakeTimers ( )
44
49
const { container} = render ( `<div></div>` )
45
- const start = performance . now ( )
50
+ const startFake = performance . now ( )
46
51
// there's a bug with this rule here...
47
52
// eslint-disable-next-line jest/valid-expect
48
53
await expect (
@@ -51,7 +56,13 @@ test('times out after 1000ms by default', async () => {
51
56
`Timed out in waitForElementToBeRemoved.` ,
52
57
)
53
58
// NOTE: this assertion ensures that the timeout runs in the declared (fake) clock.
54
- expect ( performance . now ( ) - start ) . toBeGreaterThanOrEqual ( 1000 )
59
+ expect ( performance . now ( ) - startFake ) . toBeGreaterThanOrEqual ( 1000 )
60
+ jest . useRealTimers ( )
61
+ // NOTE: this assertion ensures that the timeout runs in the declared (fake) clock
62
+ // while in real time the time was only a fraction since the real clock is only bound by the CPU.
63
+ // So 20ms is really just an approximation on how long the CPU needs to execute our code.
64
+ // If people want to timeout in real time they should rely on their test runners.
65
+ expect ( performance . now ( ) - startReal ) . toBeLessThanOrEqual ( 20 )
55
66
} )
56
67
57
68
test ( 'recursive timers do not cause issues' , async ( ) => {
0 commit comments