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
Copy file name to clipboardExpand all lines: docs/api/vi.md
+69-3Lines changed: 69 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -254,10 +254,10 @@ import { vi } from 'vitest'
254
254
```ts
255
255
// increment.test.js
256
256
import { vi } from'vitest'
257
-
257
+
258
258
// axios is a default export from `__mocks__/axios.js`
259
259
importaxiosfrom'axios'
260
-
260
+
261
261
// increment is a named export from `src/__mocks__/increment.js`
262
262
import { increment } from'../increment.js'
263
263
@@ -371,7 +371,7 @@ test('importing the next module imports mocked one', async () => {
371
371
372
372
```ts
373
373
import { vi } from'vitest'
374
-
374
+
375
375
import { data } from'./data.js'// Will not get reevaluated beforeEach test
376
376
377
377
beforeEach(() => {
@@ -706,8 +706,74 @@ unmockedIncrement(30) === 31
706
706
707
707
The implementation is based internally on [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers).
708
708
709
+
## vi.isFakeTimers
710
+
711
+
-**Type:**`() => boolean`
712
+
-**Version:** Since Vitest 0.34.5
713
+
714
+
Returns `true` if fake timers are enabled.
715
+
709
716
## vi.useRealTimers
710
717
711
718
-**Type:**`() => Vitest`
712
719
713
720
When timers are run out, you may call this method to return mocked timers to its original implementations. All timers that were run before will not be restored.
721
+
722
+
### vi.waitFor
723
+
724
+
-**Type:**`function waitFor<T>(callback: WaitForCallback<T>, options?: number | WaitForOptions): Promise<T>`
725
+
-**Version**: Since Vitest 0.34.5
726
+
727
+
Wait for the callback to execute successfully. If the callback throws an error or returns a rejected promise it will continue to wait until it succeeds or times out.
728
+
729
+
This is very useful when you need to wait for some asynchronous action to complete, for example, when you start a server and need to wait for it to start.
730
+
731
+
```ts
732
+
import { test, vi } from'vitest'
733
+
734
+
test('Server started successfully', async () => {
735
+
let server =false
736
+
737
+
setTimeout(() => {
738
+
server=true
739
+
}, 100)
740
+
741
+
function checkServerStart() {
742
+
if (!server)
743
+
thrownewError('Server not started')
744
+
745
+
console.log('Server started')
746
+
}
747
+
748
+
const res =awaitvi.waitFor(checkServerStart, {
749
+
timeout: 500, // default is 1000
750
+
interval: 20, // default is 50
751
+
})
752
+
expect(server).toBe(true)
753
+
})
754
+
```
755
+
756
+
It also works for asynchronous callbacks
757
+
758
+
```ts
759
+
import { test, vi } from'vitest'
760
+
761
+
test('Server started successfully', async () => {
762
+
asyncfunction startServer() {
763
+
returnnewPromise((resolve) => {
764
+
setTimeout(() => {
765
+
server=true
766
+
resolve('Server started')
767
+
}, 100)
768
+
})
769
+
}
770
+
771
+
const server =awaitvi.waitFor(startServer, {
772
+
timeout: 500, // default is 1000
773
+
interval: 20, // default is 50
774
+
})
775
+
expect(server).toBe('Server started')
776
+
})
777
+
```
778
+
779
+
If `vi.useFakeTimers` is used, `vi.waitFor` automatically calls `vi.advanceTimersByTime(interval)` in every check callback.
0 commit comments