You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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@108efae
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@
3
3
### Features
4
4
5
5
-`[jest-config]` Add `defineConfig` and `mergeConfig` helpers for type-safe Jest config ([#15844](https://github.com/jestjs/jest/pull/15844))
6
+
-`[jest-fake-timers]` Add `setTimerTickMode` to configure how timers advance
6
7
7
8
### Fixes
8
9
@@ -12,6 +13,7 @@
12
13
### Chore & Maintenance
13
14
14
15
-`[docs]` Update V30 migration guide to notify users on `jest.mock()` work with case-sensitive path ([#15849](https://github.com/jestjs/jest/pull/15849))
Copy file name to clipboardExpand all lines: docs/JestObjectAPI.md
+23Lines changed: 23 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1126,6 +1126,29 @@ This function is not available when using legacy fake timers implementation.
1126
1126
1127
1127
:::
1128
1128
1129
+
### `jest.setTimerTickMode(mode)`
1130
+
1131
+
Allows configuring how fake timers advance time.
1132
+
1133
+
Configuration options:
1134
+
1135
+
```ts
1136
+
typeTimerTickMode=
1137
+
| {mode:'manual'}
1138
+
| {mode:'nextAsync'}
1139
+
| {mode:'interval'; delta?:number};
1140
+
```
1141
+
1142
+
-`manual`: Timers do not advance without explicit, manual calls to the tick APIs (`jest.advanceTimersByTime(ms)`, `jest.runAllTimers()`, etc).
1143
+
-`nextAsync`: The clock will continuously break the event loop, then run the next timer until the mode changes.
1144
+
-`interval`: This is the same as specifying `advanceTimers: true` with an `advanceTimeDelta`. If the delta is not specified, 20 will be used by default.
1145
+
1146
+
:::info
1147
+
1148
+
This function is not available when using legacy fake timers implementation.
1149
+
1150
+
:::
1151
+
1129
1152
### `jest.getRealSystemTime()`
1130
1153
1131
1154
When mocking time, `Date.now()` will also be mocked. If you for some reason need access to the real current time, you can invoke this function.
0 commit comments