From 06115f376a6ff9fb6a30c3a7842818c991a99950 Mon Sep 17 00:00:00 2001 From: Johan Nyman Date: Wed, 27 Sep 2023 16:04:05 +0200 Subject: [PATCH] chore: improve unit test error logging in waitUntil() --- tests/internal-tests/src/__tests__/lib/lib.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/internal-tests/src/__tests__/lib/lib.ts b/tests/internal-tests/src/__tests__/lib/lib.ts index 8baf41fe..6156e5bf 100644 --- a/tests/internal-tests/src/__tests__/lib/lib.ts +++ b/tests/internal-tests/src/__tests__/lib/lib.ts @@ -7,6 +7,8 @@ export function waitTime(ms: number): Promise { * Useful in unit-tests as a way to wait until a predicate is fulfilled. */ export async function waitUntil(expectFcn: () => void, maxWaitTime: number): Promise { + const previousErrors: string[] = [] + const startTime = Date.now() while (true) { await waitTime(100) @@ -14,8 +16,22 @@ export async function waitUntil(expectFcn: () => void, maxWaitTime: number): Pro 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 } }