Description
Two tests in `tests/unit/MiddlewareTest.ts` are flaky and fail intermittently in CI:
- `Middleware › HandleUnusedOptimisticID › Request with preexistingReportID` (line 149)
- `Middleware › HandleUnusedOptimisticID › OpenReport to a chat with preexistingReportID and clean up optimistic participant data` (line 343)
Both fail on the same assertion:
```
expect(global.fetch).toHaveBeenCalledTimes(2)
```
Evidence
Observed in CI run #33684 (attempt 1 failed, attempt 2 passed — confirming this is a flaky/timing issue).
The logs also report a worker teardown leak:
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks. Active timers can also cause this.
Likely cause
The tests use `waitForNetworkPromises()` / `waitForBatchedUpdates()` before asserting on `global.fetch` call count. A race condition between the `SequentialQueue.unpause()` call and the fetch mock resolution can cause the second fetch to fire after the assertion runs. The worker teardown leak suggests an open handle (timer or promise) that affects subsequent test isolation.
Upwork Automation - Do Not Edit
- Upwork Job URL: https://www.upwork.com/jobs/~022054904870462893459
- Upwork Job ID: 2054904870462893459
- Last Price Increase: 2026-05-14
Issue Owner
Current Issue Owner: @mallenexpensify
Description
Two tests in `tests/unit/MiddlewareTest.ts` are flaky and fail intermittently in CI:
Both fail on the same assertion:
```
expect(global.fetch).toHaveBeenCalledTimes(2)
```
Evidence
Observed in CI run #33684 (attempt 1 failed, attempt 2 passed — confirming this is a flaky/timing issue).
The logs also report a worker teardown leak:
Likely cause
The tests use `waitForNetworkPromises()` / `waitForBatchedUpdates()` before asserting on `global.fetch` call count. A race condition between the `SequentialQueue.unpause()` call and the fetch mock resolution can cause the second fetch to fire after the assertion runs. The worker teardown leak suggests an open handle (timer or promise) that affects subsequent test isolation.
Upwork Automation - Do Not Edit
Issue Owner
Current Issue Owner: @mallenexpensify