Код: Выделить всё
import { describe, it, afterEach, beforeEach, vi } from 'vitest'
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function countDown(timesLeft) {
while (timesLeft > 0) {
console.log("Counting down from " + timesLeft)
await sleep(1000)
timesLeft--
}
}
describe('countDown', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
})
it('works with runAllTimers', async () => {
const promise = countDown(2)
vi.runAllTimers()
await promise
})
})
Подробнее здесь: https://stackoverflow.com/questions/796 ... settimeout