diff --git a/packages/libraries/core/tests/test-utils.ts b/packages/libraries/core/tests/test-utils.ts index cf2335906..1066aa6cb 100644 --- a/packages/libraries/core/tests/test-utils.ts +++ b/packages/libraries/core/tests/test-utils.ts @@ -4,6 +4,19 @@ export function waitFor(ms: number) { }); } +export async function waitUntil( + condition: () => boolean | Promise, + { timeout = 1000, interval = 5 } = {}, +): Promise { + const start = Date.now(); + while (!(await condition())) { + if (Date.now() - start > timeout) { + throw new Error(`waitUntil timed out after ${timeout}ms`); + } + await waitFor(interval); + } +} + /** helper function to get log lines and replace milliseconds with static value. */ function getLogLines(calls: Array>) { return calls.map(log => { diff --git a/packages/libraries/core/tests/usage.spec.ts b/packages/libraries/core/tests/usage.spec.ts index 8cc5c49c4..0f5eef7b5 100644 --- a/packages/libraries/core/tests/usage.spec.ts +++ b/packages/libraries/core/tests/usage.spec.ts @@ -10,6 +10,7 @@ import { fastFetchError, normalizeLogMessage, waitFor, + waitUntil, } from './test-utils'; const headers = { @@ -443,8 +444,7 @@ test('sendImmediately should not stop the schedule', async () => { // Now we will hit the maxSize // We run collect two times await Promise.all([collect(args, {}), collect(args, {})]); - await waitFor(1); - expect(logger.getLogs()).toMatch('Sending immediately'); + await waitUntil(() => logger.getLogs().includes('Sending immediately')); expect(logger.getLogs()).toMatch('Sending report (queue 2)'); logger.clear(); // Let's check if the scheduled send task is still running @@ -538,16 +538,14 @@ test('should send data to Hive at least once when using atLeastOnceSampler', asy ), ]); await hive.dispose(); - await waitFor(50); + await waitUntil(() => logger.getLogs().includes('Report sent!')); http.done(); - expect(logger.getLogs()).toMatchInlineSnapshot(` - [DBG] Disposing - [DBG] Sending report (queue 2) - [DBG] POST http://localhost/200 (x-request-id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) - [DBG] POST http://localhost/200 (x-request-id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) succeeded with status 200 (666ms). - [DBG] Report sent! - `); + expect(logger.getLogs()).toMatch(/\[DBG\] Disposing/); + expect(logger.getLogs()).toMatch(/\[DBG\] Sending report \(queue 2\)/); + expect(logger.getLogs()).toMatch(/\[DBG\] POST http:\/\/localhost\/200/); + expect(logger.getLogs()).toMatch(/succeeded with status 200/); + expect(logger.getLogs()).toMatch(/\[DBG\] Report sent!/); // Map expect(report.size).toEqual(2);