From 55caf313a23a74bbfec9572c245352dab37b39f8 Mon Sep 17 00:00:00 2001 From: Rowan Manning <138944+rowanmanning@users.noreply.github.com> Date: Thu, 13 Feb 2025 09:41:39 +0000 Subject: [PATCH] test: wait for logs to appear in end-to-end test This test has been very flaky, I think this is because Pino uses async logs and sometimes the log doesn't appear immediately. This should address the race condition where our tests run before the log has had time to come through. --- .../test/end-to-end/index.spec.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/middleware-log-errors/test/end-to-end/index.spec.js b/packages/middleware-log-errors/test/end-to-end/index.spec.js index ed51f2be..d986894b 100644 --- a/packages/middleware-log-errors/test/end-to-end/index.spec.js +++ b/packages/middleware-log-errors/test/end-to-end/index.spec.js @@ -1,4 +1,5 @@ const { fork } = require('node:child_process'); +const { setTimeout } = require('node:timers/promises'); describe('@dotcom-reliability-kit/middleware-log-errors end-to-end', () => { let child; @@ -28,6 +29,19 @@ describe('@dotcom-reliability-kit/middleware-log-errors end-to-end', () => { describe('GET /error', () => { beforeAll(async () => { await fetch(`${baseUrl}/error`); + + // Pino uses async logs so we can't guarantee that our log will have been output + // immediately. We wait a total of 5s for the log to come through, checking for + // the log in 100ms increments + let waitTime = 0; + while (waitTime < 5000) { + if (stdout.includes('HANDLED_ERROR')) { + return; + } + waitTime += 100; + await setTimeout(100); + } + throw new Error('HANDLED_ERROR log took too long to come through'); }); it('logs error information to stdout', () => {