Skip to content

Commit

Permalink
chore: improve unit test error logging in waitUntil()
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Sep 27, 2023
1 parent 409846d commit 06115f3
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tests/internal-tests/src/__tests__/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,31 @@ export function waitTime(ms: number): Promise<void> {
* Useful in unit-tests as a way to wait until a predicate is fulfilled.
*/
export async function waitUntil(expectFcn: () => void, maxWaitTime: number): Promise<void> {
const previousErrors: string[] = []

const startTime = Date.now()
while (true) {
await waitTime(100)
try {
expectFcn()
return
} catch (err) {
const errorStr = `${err}`
if (previousErrors.length) {
const previousError = previousErrors[previousErrors.length - 1]
if (errorStr !== previousError) {
previousErrors.push(errorStr)
}
} else {
previousErrors.push(errorStr)
}

let waitedTime = Date.now() - startTime
if (waitedTime > maxWaitTime) throw err
if (waitedTime > maxWaitTime) {
console.log(`Previous errors: \n${previousErrors.join('\n')}`)

throw err
}
// else ignore error and try again later
}
}
Expand Down

0 comments on commit 06115f3

Please sign in to comment.