Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/lib/utils/waitUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const waitAndRetry = <T>(
function performCheck() {
if (maxRetryCount !== 0 && retryCount > maxRetryCount) {
resolve(predicateFn())
return
}
Promise.resolve()
.then(() => {
Expand Down
57 changes: 57 additions & 0 deletions packages/core/test/utils/waitUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { setTimeout } from 'node:timers/promises'
import { describe, expect, it } from 'vitest'

import { waitAndRetry } from '../../lib/utils/waitUtils.ts'

describe('waitUtils', () => {
describe('waitAndRetry', () => {
it('resolves with the predicate result once it is truthy', async () => {
let calls = 0

const result = await waitAndRetry(() => {
calls++
return calls === 3 ? 'done' : undefined
}, 1)

expect(result).toBe('done')
expect(calls).toBe(3)
})

it('stops polling after the retries are exhausted', async () => {
let calls = 0

const result = await waitAndRetry(
() => {
calls++
return false
},
1,
3,
)

expect(result).toBe(false)
// 4 checks that count as retries, plus the final check whose result is returned
expect(calls).toBe(5)

await setTimeout(50)
expect(calls).toBe(5)
})

it('rejects when the final check after the retries are exhausted rejects', async () => {
let calls = 0

const promise = waitAndRetry(
() => {
calls++
return calls > 4 ? Promise.reject(new Error('unreachable')) : Promise.resolve(false)
},
1,
3,
)

await expect(promise).rejects.toThrow('unreachable')
await setTimeout(50)
expect(calls).toBe(5)
})
})
})