Skip to content

Fix flaky MiddlewareTest assertions after HandleUnusedOptimisticID rewrites queued request#90762

Closed
ykd007 wants to merge 1 commit into
Expensify:mainfrom
ykd007:fix/middleware-flaky-test-90660
Closed

Fix flaky MiddlewareTest assertions after HandleUnusedOptimisticID rewrites queued request#90762
ykd007 wants to merge 1 commit into
Expensify:mainfrom
ykd007:fix/middleware-flaky-test-90660

Conversation

@ykd007

@ykd007 ykd007 commented May 15, 2026

Copy link
Copy Markdown

$ #90660

Root cause

When HandleUnusedOptimisticID detects a preexistingReportID in the first response, it rewrites the subsequent pending request and re-enqueues it. This rewrite path goes through an async Onyx.merge call before the second fetch fires. A single waitForNetworkPromises() / waitForBatchedUpdates() resolves before that second round-trip completes, so the toHaveBeenCalledTimes(2) assertion occasionally runs when only 1 fetch has happened — the classic timing flap.

Fix

Add a second waitForNetworkPromises() after the initial flush in the two affected tests. This mirrors the standard multi-round-trip pattern used elsewhere in the test suite and ensures the requeued fetch has fully settled before the assertion runs.

The teardown worker leak reported in the issue is a downstream symptom of the same open promise — the extra flush also lets the test exit cleanly.

Tests

The two tests from HandleUnusedOptimisticID:

  • Request with preexistingReportID
  • OpenReport to a chat with preexistingReportID and clean up optimistic participant data

…eportID handling

When HandleUnusedOptimisticID rewrites a pending request after seeing
preexistingReportID in the first response, the rewrite goes through an
async Onyx.merge round-trip before re-enqueuing. A single
waitForNetworkPromises() or waitForBatchedUpdates() resolved before the
requeued fetch fired, causing the fetch count assertion to flap.

Adding a second waitForNetworkPromises() after the initial flush ensures
the re-dispatched request has actually completed before asserting on
global.fetch call count.
@ykd007 ykd007 requested a review from a team as a code owner May 15, 2026 10:25
@github-actions

Copy link
Copy Markdown
Contributor

👋 Hi @ykd007, thanks for your interest in contributing to Expensify!

This PR has been automatically closed because it doesn't appear to meet our contribution requirements:

  • You are not a member of the Expensify GitHub organization
  • No linked GitHub issue was found in the PR description where you are listed as an assignee
  • No linked GitHub PR was found in the PR description where you are the author or a reviewer

If you'd like to contribute, please make sure to:

  1. Find an open issue you'd like to work on
  2. Get assigned to the issue by following our contribution process
  3. Link the issue in your PR description using the format: $ https://github.com/Expensify/App/issues/<issueID>

Please review our contributing guidelines for more details.

If you believe this was closed in error, please reach out in the #expensify-open-source Slack channel.

@github-actions github-actions Bot closed this May 15, 2026
@github-actions github-actions Bot locked as spam and limited conversation to collaborators May 15, 2026
@melvin-bot melvin-bot Bot requested review from youssef-lr and removed request for a team May 15, 2026 10:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant