diff --git a/tests/async/parallel.test.ts b/tests/async/parallel.test.ts index efe0f57d..ba42126b 100644 --- a/tests/async/parallel.test.ts +++ b/tests/async/parallel.test.ts @@ -2,13 +2,10 @@ import * as _ from 'radashi' import { AggregateError } from 'radashi' describe('parallel', () => { - beforeEach(() => { - vi.useFakeTimers({ shouldAdvanceTime: true }) - }) test('returns all results from all functions', async () => { const [errors, results] = await _.try(async () => { return _.parallel(1, _.list(1, 3), async num => { - await _.sleep(1000) + await _.sleep(0) return `hi_${num}` }) })() @@ -18,7 +15,7 @@ describe('parallel', () => { test('throws errors as array of all errors', async () => { const [error, results] = await _.try(async () => { return _.parallel(1, _.list(1, 3), async num => { - await _.sleep(1000) + await _.sleep(0) if (num === 2) { throw new Error('number is 2') } @@ -36,7 +33,7 @@ describe('parallel', () => { await _.parallel(3, _.list(1, 14), async () => { numInProgress++ tracking.push(numInProgress) - await _.sleep(300) + await _.sleep(0) numInProgress-- }) expect(Math.max(...tracking)).toBe(3) diff --git a/tests/async/retry.test.ts b/tests/async/retry.test.ts index acf6ac2e..05658121 100644 --- a/tests/async/retry.test.ts +++ b/tests/async/retry.test.ts @@ -5,7 +5,7 @@ const cast = (value: any): T => value describe('retry', () => { beforeEach(() => { - vi.useFakeTimers({ shouldAdvanceTime: true }) + vi.useFakeTimers() }) test('returns result of given function', async () => { const result = await _.retry(cast(null), async _bail => { @@ -69,7 +69,9 @@ describe('retry', () => { const func = async () => { throw 'quitagain' } - await _.retry({ delay: 100 }, func) + const promise = _.retry({ delay: 1000, times: 2 }, func) + vi.advanceTimersByTimeAsync(1000) + await promise } catch (err) { expect(err).toBe('quitagain') return @@ -80,7 +82,7 @@ describe('retry', () => { let count = 0 let backoffs = 0 const start = Date.now() - await _.retry( + const promise = _.retry( { times: 3, backoff: i => { @@ -95,13 +97,19 @@ describe('retry', () => { } }, ) + // Two async advances for the first two async attempts, each of which are + // followed by sleeps + vi.advanceTimersToNextTimerAsync() + vi.advanceTimersToNextTimerAsync() + await promise + const diff = Date.now() - start expect(count).toBe(3) // Time taken should at least be the // total ms backed off. Using exponential // backoff (above) 3 times (passing on // the third try) that is: - // - 10**1 + 10**2 = 1025 + // - 1**10 + 2**10 = 1025 // The performance typically comes in 1 // or 2 milliseconds after. expect(diff).toBeGreaterThanOrEqual(backoffs) diff --git a/tests/async/sleep.test.ts b/tests/async/sleep.test.ts index 805926e3..a4050081 100644 --- a/tests/async/sleep.test.ts +++ b/tests/async/sleep.test.ts @@ -2,12 +2,14 @@ import * as _ from 'radashi' describe('sleep', () => { beforeEach(() => { - vi.useFakeTimers({ shouldAdvanceTime: true }) + vi.useFakeTimers() }) test('suspends a thread for a specified number of milliseconds', async () => { const ONE_SECOND = 1000 const before = Date.now() - await _.sleep(ONE_SECOND) + const promise = _.sleep(ONE_SECOND) + vi.advanceTimersToNextTimerAsync() + await promise const after = Date.now() expect(after).toBeGreaterThanOrEqual(before + ONE_SECOND) }) diff --git a/tests/curry/debounce.test.ts b/tests/curry/debounce.test.ts index 559d3b72..e9513882 100644 --- a/tests/curry/debounce.test.ts +++ b/tests/curry/debounce.test.ts @@ -9,9 +9,11 @@ describe('debounce', () => { func() func() } + const delay = 600 beforeEach(() => { - func = _.debounce({ delay: 600 }, mockFunc) + vi.useFakeTimers() + func = _.debounce({ delay }, mockFunc) }) afterEach(() => { @@ -21,7 +23,7 @@ describe('debounce', () => { test('only executes once when called rapidly', async () => { runFunc3Times() expect(mockFunc).toHaveBeenCalledTimes(0) - await _.sleep(610) + vi.advanceTimersByTime(delay + 10) expect(mockFunc).toHaveBeenCalledTimes(1) }) @@ -47,7 +49,7 @@ describe('debounce', () => { expect(mockFunc).toHaveBeenCalledTimes(1) func() expect(mockFunc).toHaveBeenCalledTimes(1) - await _.sleep(610) + vi.advanceTimersByTime(delay + 10) expect(mockFunc).toHaveBeenCalledTimes(2) func.flush() expect(mockFunc).toHaveBeenCalledTimes(3) @@ -58,11 +60,11 @@ describe('debounce', () => { func() results.push(func.isPending()) results.push(func.isPending()) - await _.sleep(610) + vi.advanceTimersByTime(delay + 10) results.push(func.isPending()) func() results.push(func.isPending()) - await _.sleep(610) + vi.advanceTimersByTime(delay + 10) results.push(func.isPending()) assert.deepEqual(results, [true, true, false, true, false]) }) @@ -70,7 +72,7 @@ describe('debounce', () => { test('returns if there is any pending invocation when the pending method is called', async () => { func() func.cancel() - await _.sleep(610) + vi.advanceTimersByTime(delay + 10) expect(mockFunc).toHaveBeenCalledTimes(0) }) }) diff --git a/tests/curry/memo.test.ts b/tests/curry/memo.test.ts index d76284d3..e2f8d11f 100644 --- a/tests/curry/memo.test.ts +++ b/tests/curry/memo.test.ts @@ -24,20 +24,22 @@ describe('memo', () => { expect(resultB).not.toBe(resultA) }) test('calls function again when first value expires', async () => { + vi.useFakeTimers() const func = _.memo(() => new Date().getTime(), { ttl: 1, }) const resultA = func() - await new Promise(res => setTimeout(res, 100)) + vi.advanceTimersByTime(100) const resultB = func() expect(resultA).not.toBe(resultB) }) test('does not call function again when first value has not expired', async () => { + vi.useFakeTimers() const func = _.memo(() => new Date().getTime(), { ttl: 1000, }) const resultA = func() - await new Promise(res => setTimeout(res, 100)) + vi.advanceTimersByTime(100) const resultB = func() expect(resultA).toBe(resultB) }) diff --git a/tests/curry/throttle.test.ts b/tests/curry/throttle.test.ts index a46e3034..edfacce1 100644 --- a/tests/curry/throttle.test.ts +++ b/tests/curry/throttle.test.ts @@ -1,14 +1,20 @@ import * as _ from 'radashi' describe('throttle', () => { + const interval = 600 + + beforeEach(() => { + vi.useFakeTimers() + }) + test('throttles!', async () => { let calls = 0 - const func = _.throttle({ interval: 600 }, () => calls++) + const func = _.throttle({ interval }, () => calls++) func() func() func() expect(calls).toBe(1) - await _.sleep(610) + vi.advanceTimersByTime(interval + 10) func() func() func() @@ -17,7 +23,7 @@ describe('throttle', () => { test('returns if the throttle is active', async () => { const results = [] - const func = _.throttle({ interval: 600 }, () => {}) + const func = _.throttle({ interval }, () => {}) results.push(func.isThrottled()) func() results.push(func.isThrottled()) @@ -25,7 +31,7 @@ describe('throttle', () => { results.push(func.isThrottled()) func() results.push(func.isThrottled()) - await _.sleep(610) + vi.advanceTimersByTime(interval + 10) results.push(func.isThrottled()) assert.deepEqual(results, [false, true, true, true, false]) })