From e07fa5adcc7b0b418685caf849b982c5d6c15156 Mon Sep 17 00:00:00 2001 From: Francesco Gringl-Novy Date: Wed, 29 Apr 2026 11:09:23 +0200 Subject: [PATCH] test(node): Fix flaky ANR test Closes https://github.com/getsentry/sentry-javascript/issues/20268 --- .../suites/anr/test.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/dev-packages/node-core-integration-tests/suites/anr/test.ts b/dev-packages/node-core-integration-tests/suites/anr/test.ts index c9a81ccb5db0..406830c9b299 100644 --- a/dev-packages/node-core-integration-tests/suites/anr/test.ts +++ b/dev-packages/node-core-integration-tests/suites/anr/test.ts @@ -2,6 +2,17 @@ import type { Event } from '@sentry/core'; import { afterAll, describe, expect, test } from 'vitest'; import { cleanupChildProcesses, createRunner } from '../../utils/runner'; +/** Avoid flakes on slow CI: fixed sleeps can fire before the child process has finished exiting. */ +async function waitForChildExit(childHasExited: () => boolean, timeoutMs = 30_000): Promise { + const start = Date.now(); + while (!childHasExited()) { + if (Date.now() - start > timeoutMs) { + throw new Error('Timed out waiting for child process to exit'); + } + await new Promise(resolve => setTimeout(resolve, 100)); + } +} + const ANR_EVENT = { // Ensure we have context contexts: { @@ -178,7 +189,7 @@ describe('should report ANR when event loop blocked', { timeout: 90_000 }, () => test('should exit', async () => { const runner = createRunner(__dirname, 'should-exit.js').start(); - await new Promise(resolve => setTimeout(resolve, 5_000)); + await waitForChildExit(() => runner.childHasExited()); expect(runner.childHasExited()).toBe(true); }); @@ -186,7 +197,7 @@ describe('should report ANR when event loop blocked', { timeout: 90_000 }, () => test('should exit forced', async () => { const runner = createRunner(__dirname, 'should-exit-forced.js').start(); - await new Promise(resolve => setTimeout(resolve, 5_000)); + await waitForChildExit(() => runner.childHasExited()); expect(runner.childHasExited()).toBe(true); });