docs: add section around the usage of fake timers in rendered unit tests

fixes https://github.com/podman-desktop/podman-desktop/issues/14547
Signed-off-by: Florent Benoit <fbenoit@redhat.com>
This commit is contained in:
Florent Benoit 2025-10-23 10:53:28 +02:00 committed by Florent BENOIT
parent df441cbd26
commit b0a4306ad7

View file

@ -283,3 +283,36 @@ test('compo1 calls fct1 of obj on mount', async () => {
expect(obj.fct1).toHaveBeenCalledWith('a name'); // check the method has been called
});
```
### Using Fake Timers with Svelte Components
When testing Svelte components in the `packages/renderer` package, **always enable automatic time advancement** by using:
```ts
vi.useFakeTimers({ shouldAdvanceTime: true });
```
Avoid calling `vi.useFakeTimers()` without options.
If `shouldAdvanceTime` is not enabled, fake timers will **completely freeze time**, which can lead to deadlocks when:
- Sveltes internal async updates wait for the next event loop tick
- Testing Librarys async queries (`findBy*`, `waitFor`) continuously poll for elements
By setting `shouldAdvanceTime: true`, timers will automatically advance during pending async operations. This prevents hangs while still allowing manual time control with `vi.advanceTimersByTime()`.
✅ **Use this pattern:**
```ts
beforeEach(() => {
vi.useFakeTimers({ shouldAdvanceTime: true });
});
```
🚫 **Instead of:**
```ts
afterEach(() => {
vi.useRealTimers();
});
```