Skip to content

Commit fc4133f

Browse files
committed
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.
1 parent 60ea1ef commit fc4133f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

packages/middleware-log-errors/test/end-to-end/index.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { fork } = require('node:child_process');
2+
const { setTimeout } = require('node:timers/promises');
23

34
describe('@dotcom-reliability-kit/middleware-log-errors end-to-end', () => {
45
let child;
@@ -28,6 +29,19 @@ describe('@dotcom-reliability-kit/middleware-log-errors end-to-end', () => {
2829
describe('GET /error', () => {
2930
beforeAll(async () => {
3031
await fetch(`${baseUrl}/error`);
32+
33+
// Pino uses async logs so we can't guarantee that our log will have been output
34+
// immediately. We wait a total of 5s for the log to come through, checking for
35+
// the log in 100ms increments
36+
let waitTime = 0;
37+
while (waitTime < 5000) {
38+
if (stdout.includes('HANDLED_ERROR')) {
39+
return;
40+
}
41+
waitTime += 100;
42+
await setTimeout(100);
43+
}
44+
throw new Error('HANDLED_ERROR log took too long to come through');
3145
});
3246

3347
it('logs error information to stdout', () => {

0 commit comments

Comments
 (0)