Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

sleep.test.ts

24 lines560 Bytes

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { sleep } from '../sleep'
3​
4describe('sleep', () => {
5 beforeEach(() => {
6 vi.useFakeTimers()
7 })
8​
9 afterEach(() => {
10 vi.useRealTimers()
11 })
12​
13 it('should sleep for the given amount of time', async () => {
14 const onResolve = vi.fn<() => void>()
15​
16 void sleep(100).then(onResolve)
17​
18 await vi.advanceTimersByTimeAsync(99)
19 expect(onResolve).not.toHaveBeenCalled()
20​
21 await vi.advanceTimersByTimeAsync(1)
22 expect(onResolve).toHaveBeenCalledTimes(1)
23 })
24})