@@ -10,11 +10,8 @@ flaky.
10
10
11
11
To solve these problems, or if you need to rely on specific timestamps in your
12
12
code, most testing frameworks offer the option to replace the real timers in
13
- your tests with fake ones. This should be used sporadically and not on a regular
14
- basis since using it contains some overhead.
15
-
16
- When using fake timers in your tests, all of the code inside your test uses fake
17
- timers.
13
+ your tests with fake ones. This has a side effect - when using fake timers in
14
+ your tests, _ all_ of the code inside your test uses fake timers.
18
15
19
16
The common pattern to setup fake timers is usually within the ` beforeEach ` , for
20
17
example:
@@ -26,12 +23,10 @@ beforeEach(() => {
26
23
})
27
24
```
28
25
29
- When using fake timers, you need to remember to restore the timers after your
30
- test runs.
31
-
32
- The main reason to do that is to prevent 3rd party libraries running after your
33
- test finishes (e.g cleanup functions), from being coupled to your fake timers
34
- and use real timers instead.
26
+ Since fake timers are mocking native timer functions, it is necessary to restore
27
+ the timers after your test runs, just like regular mocks. This prevents fake
28
+ timers leaking into other test cases and cleanup functions, where real timers
29
+ are expected.
35
30
36
31
For that you usually call ` useRealTimers ` in ` afterEach ` .
37
32
@@ -51,3 +46,11 @@ afterEach(() => {
51
46
jest .useRealTimers ()
52
47
})
53
48
```
49
+
50
+ :::note
51
+
52
+ Combining fake timers with ` user-event ` may cause test timeouts. Refer to
53
+ [ ` advanceTimers ` ] ( user-event/options.mdx#advancetimers ) option to prevent this
54
+ issue.
55
+
56
+ :::
0 commit comments