Skip to content

Commit

Permalink
docs(api): add advanceTimersByTimeAsync, advanceTimersToNextTimerAsyn…
Browse files Browse the repository at this point in the history
…c and runOnlyPendingTimersAsync
  • Loading branch information
guillaumeduboc committed Jan 11, 2023
1 parent 8a9b59a commit f86e666
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions docs/api/index.md
Expand Up @@ -2533,6 +2533,19 @@ Vitest provides utility functions to help you out through it's **vi** helper. Yo
vi.advanceTimersByTime(150)
```

### vi.advanceTimersByTimeAsync

- **Type:** `async (ms: number) => Promise<Vitest>`

Works just like `runAllTimersAsync`, but will end after passed milliseconds. This will include asynchronously set timers. For example this will log `1, 2, 3` and will not throw:

```ts
let i = 0
setInterval(() => Promise.resolve().then(() => console.log(++i)), 50)

await vi.advanceTimersByTimeAsync(150)
```

### vi.advanceTimersToNextTimer

- **Type:** `() => Vitest`
Expand All @@ -2548,6 +2561,21 @@ Vitest provides utility functions to help you out through it's **vi** helper. Yo
.advanceTimersToNextTimer() // log 3
```

### vi.advanceTimersToNextTimerAsync

- **Type:** `async () => Promise<Vitest>`

Will call next available timer even if it was set asynchronously. Useful to make assertions between each timer call. You can chain call it to manage timers by yourself.

```ts
let i = 0
setInterval(() => Promise.resolve().then(() => console.log(++i)), 50)

vi.advanceTimersToNextTimerAsync() // log 1
.advanceTimersToNextTimerAsync() // log 2
.advanceTimersToNextTimerAsync() // log 3
```

### vi.getTimerCount

- **Type:** `() => number`
Expand Down Expand Up @@ -2996,6 +3024,28 @@ IntersectionObserver === undefined
vi.runOnlyPendingTimers()
```

### vi.runOnlyPendingTimersAsync

- **Type:** `async () => Promise<Vitest>`

This method will asynchronously call every timer that was initiated after `vi.useFakeTimers()` call, even asynchronous ones. It will not fire any timer that was initiated during its call. For example this will log `1, 2, 3`:

```ts
setTimeout(() => {
console.log(1)
}, 100)
setTimeout(() => {
Promise.resolve().then(() => {
console.log(2)
setTimeout(() => {
console.log(3)
}, 50)
})
}, 10)

await vi.runOnlyPendingTimersAsync()
```

### vi.setSystemTime

- **Type**: `(date: string | number | Date) => void`
Expand Down

0 comments on commit f86e666

Please sign in to comment.