docs: improve clock guide (#31487)

This commit is contained in:
Debbie O'Brien 2024-06-28 22:04:39 +02:00 committed by GitHub
parent f46ae15500
commit ea33137a0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,6 +8,18 @@ import LiteYouTube from '@site/src/components/LiteYouTube';
Accurately simulating time-dependent behavior is essential for verifying the correctness of applications. Utilizing [Clock] functionality allows developers to manipulate and control time within tests, enabling the precise validation of features such as rendering time, timeouts, scheduled tasks without the delays and variability of real-time execution. Accurately simulating time-dependent behavior is essential for verifying the correctness of applications. Utilizing [Clock] functionality allows developers to manipulate and control time within tests, enabling the precise validation of features such as rendering time, timeouts, scheduled tasks without the delays and variability of real-time execution.
The [Clock] API provides the following methods to control time:
- `setFixedTime`: Sets the fixed time for `Date.now()` and `new Date()`.
- `install`: initializes the clock and allows you to:
- `pauseAt`: Pauses the time at a specific time.
- `fastForward`: Fast forwards the time.
- `runFor`: Runs the time for a specific duration.
- `resume`: Resumes the time.
- `setSystemTime`: Sets the current system time.
The recommended approach is to use `setFixedTime` to set the time to a specific value. If that doesn't work for your use case, you can use `install` which allows you to pause time later on, fast forward it, tick it, etc. `setSystemTime` is only recommended for advanced use cases.
:::note
[`property: Page.clock`] overrides native global classes and functions related to time allowing them to be manually controlled: [`property: Page.clock`] overrides native global classes and functions related to time allowing them to be manually controlled:
- `Date` - `Date`
- `setTimeout` - `setTimeout`
@ -19,6 +31,7 @@ Accurately simulating time-dependent behavior is essential for verifying the cor
- `requestIdleCallback` - `requestIdleCallback`
- `cancelIdleCallback` - `cancelIdleCallback`
- `performance` - `performance`
:::
## Test with predefined time ## Test with predefined time