Commit 9097622
committed
feat(jest-fake-timers): Add feature to configure how timers advance
This change exposes the `setTimerTickMode` function from the underlying `fake-timers` library.
This is to align with the new feature introduced in `fake-timers`.
The new `setTimerTickMode` method allows developers to configure whether time should auto advance and what the delta should be after the clock has been created. Prior to this, it could only be done at creation time with `shouldAdvanceTime: true`.
This also adds a new mode for automatically advancing time that moves more quickly than the existing shouldAdvanceTime, which uses real time.
Testing with mock clocks can often turn into a real struggle when dealing with situations where some work in the test is truly async and other work is captured by the mock clock. When using mock clocks, testers are always forced to write tests with intimate knowledge of when the mock clock needs to be ticked. It is ideal for test code to be written in a way that is independent of whether a mock clock is installed.
`shouldAdvanceTime` is essentially `setInterval(() => clock.tick(ms), ms)` while this feature is `const loop = () => setTimeout(() => clock.advancetimerstoNextTimerAsync().then(() => loop()), 0);`
There are two key differences:
1. `shouldAdvanceTime` uses `clock.tick(ms)` so it synchronously runs all timers inside the "ms" of the clock queue. This doesn't allow the microtask queue to empty between the macrotask timers in the clock.
2. `shouldAdvanceTime` uses real time to advance the same amount of real time in the mock clock. `setTimerTickMode({mode: "nextAsync"})` advances time as quickly possible and as far as necessary.
See: sinonjs/fake-timers@108efae1 parent 689f340 commit 9097622
File tree
8 files changed
+165
-33
lines changed- docs
- e2e/fake-timers/set-timer-tick-mode
- __tests__
- packages
- jest-environment/src
- jest-fake-timers/src/__tests__
- jest-runtime/src
- jest-types/__typetests__
8 files changed
+165
-33
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1126 | 1126 | | |
1127 | 1127 | | |
1128 | 1128 | | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
1129 | 1152 | | |
1130 | 1153 | | |
1131 | 1154 | | |
| |||
Lines changed: 42 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
425 | 425 | | |
426 | 426 | | |
427 | 427 | | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
428 | 449 | | |
Lines changed: 48 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
77 | | - | |
78 | | - | |
| 76 | + | |
79 | 77 | | |
80 | 78 | | |
81 | 79 | | |
| |||
153 | 151 | | |
154 | 152 | | |
155 | 153 | | |
156 | | - | |
157 | | - | |
158 | | - | |
| 154 | + | |
159 | 155 | | |
160 | 156 | | |
161 | 157 | | |
| |||
186 | 182 | | |
187 | 183 | | |
188 | 184 | | |
189 | | - | |
190 | | - | |
191 | | - | |
| 185 | + | |
192 | 186 | | |
193 | 187 | | |
194 | 188 | | |
| |||
205 | 199 | | |
206 | 200 | | |
207 | 201 | | |
208 | | - | |
209 | | - | |
210 | | - | |
| 202 | + | |
211 | 203 | | |
212 | 204 | | |
213 | 205 | | |
| |||
231 | 223 | | |
232 | 224 | | |
233 | 225 | | |
234 | | - | |
235 | | - | |
236 | | - | |
| 226 | + | |
237 | 227 | | |
238 | 228 | | |
239 | 229 | | |
| |||
902 | 892 | | |
903 | 893 | | |
904 | 894 | | |
905 | | - | |
906 | | - | |
907 | | - | |
| 895 | + | |
908 | 896 | | |
909 | 897 | | |
910 | 898 | | |
| |||
1430 | 1418 | | |
1431 | 1419 | | |
1432 | 1420 | | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
1433 | 1463 | | |
1434 | 1464 | | |
1435 | 1465 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1161 | 1161 | | |
1162 | 1162 | | |
1163 | 1163 | | |
1164 | | - | |
1165 | | - | |
1166 | | - | |
1167 | | - | |
| 1164 | + | |
1168 | 1165 | | |
1169 | 1166 | | |
1170 | 1167 | | |
| |||
1337 | 1334 | | |
1338 | 1335 | | |
1339 | 1336 | | |
1340 | | - | |
1341 | | - | |
1342 | | - | |
1343 | | - | |
| 1337 | + | |
1344 | 1338 | | |
1345 | 1339 | | |
1346 | 1340 | | |
| |||
1444 | 1438 | | |
1445 | 1439 | | |
1446 | 1440 | | |
1447 | | - | |
1448 | | - | |
1449 | | - | |
| 1441 | + | |
1450 | 1442 | | |
1451 | 1443 | | |
1452 | 1444 | | |
| |||
2120 | 2112 | | |
2121 | 2113 | | |
2122 | 2114 | | |
2123 | | - | |
2124 | | - | |
2125 | | - | |
2126 | | - | |
| 2115 | + | |
2127 | 2116 | | |
2128 | 2117 | | |
2129 | 2118 | | |
| |||
2428 | 2417 | | |
2429 | 2418 | | |
2430 | 2419 | | |
| 2420 | + | |
| 2421 | + | |
| 2422 | + | |
| 2423 | + | |
| 2424 | + | |
| 2425 | + | |
| 2426 | + | |
| 2427 | + | |
| 2428 | + | |
| 2429 | + | |
| 2430 | + | |
| 2431 | + | |
| 2432 | + | |
| 2433 | + | |
| 2434 | + | |
2431 | 2435 | | |
2432 | 2436 | | |
2433 | 2437 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
651 | 651 | | |
652 | 652 | | |
653 | 653 | | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
654 | 659 | | |
655 | 660 | | |
656 | 661 | | |
| |||
0 commit comments