From f86f3526e45e089a6dd9ae6db8d6d6f765cb3c17 Mon Sep 17 00:00:00 2001 From: Yash Dhawan Date: Fri, 15 May 2026 01:27:41 +0530 Subject: [PATCH] fix(tests): resolve flaky fetch count assertions in MiddlewareTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The two HandleUnusedOptimisticID tests were intermittently failing on the assertion that global.fetch was called exactly twice. The root cause is a timing gap between the first request completing, Onyx processing the preexistingReportID update, the middleware updating the second request's data, and the second fetch actually firing. waitForNetworkPromises() alone (which runs 2 batch cycles) was not enough to flush the full async chain. Adding an extra batched-updates and network-promises pass ensures all microtasks and macro-tasks settle before we assert on call counts. Also fixes the 'OpenReport to a chat' test which used only waitForBatchedUpdates() — also insufficient for the same reason. Fixes #90660 --- tests/unit/MiddlewareTest.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/unit/MiddlewareTest.ts b/tests/unit/MiddlewareTest.ts index 535683fac096..b2444202ff4b 100644 --- a/tests/unit/MiddlewareTest.ts +++ b/tests/unit/MiddlewareTest.ts @@ -144,6 +144,12 @@ describe('Middleware', () => { })); SequentialQueue.unpause(); + // Wait for the first request to complete and for Onyx to process the preexistingReportID + // update before the second request is dispatched. A single waitForNetworkPromises() call + // is not enough because the middleware re-queues the second request after the Onyx update + // resolves, which happens in a subsequent microtask/batch cycle. + await waitForNetworkPromises(); + await waitForBatchedUpdates(); await waitForNetworkPromises(); expect(global.fetch).toHaveBeenCalledTimes(2); @@ -338,7 +344,11 @@ describe('Middleware', () => { })); SequentialQueue.unpause(); + // Both requests are sequential; we need to wait for all network activity to settle, + // including the second fetch triggered after Onyx processes preexistingReportID. + await waitForNetworkPromises(); await waitForBatchedUpdates(); + await waitForNetworkPromises(); expect(global.fetch).toHaveBeenCalledTimes(2);