Skip to content

Commit 23075d3

Browse files
authored
Merge pull request Expensify#90392 from Expensify/rory-fix-middleware-test-flakiness
[No QA] fix(test): improve MiddlewareTest isolation to prevent flaky fetch count assertion
2 parents b621e71 + f0b1469 commit 23075d3

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

tests/unit/MiddlewareTest.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Onyx from 'react-native-onyx';
22
import type {OnyxEntry} from 'react-native-onyx';
33
import SaveResponseInOnyx from '@libs/Middleware/SaveResponseInOnyx';
4+
import * as PersistedRequests from '@src/libs/actions/PersistedRequests';
45
// This import is needed to initialize the Onyx connections that call replaceOptimisticReportWithActualReport
56
import '@src/libs/actions/replaceOptimisticReportWithActualReport';
67
import HttpUtils from '@src/libs/HttpUtils';
@@ -21,18 +22,21 @@ Onyx.init({
2122
keys: ONYXKEYS,
2223
});
2324

24-
beforeAll(() => {
25-
global.fetch = TestHelper.getGlobalFetchMock();
26-
});
27-
2825
beforeEach(async () => {
2926
await Onyx.clear();
3027
await waitForBatchedUpdates();
28+
// Explicitly reset PersistedRequests module state (knownRequestIDs, ongoingRequest,
29+
// pendingSaveOperations) which Onyx.clear() alone does not fully reset.
30+
await PersistedRequests.clear();
31+
await waitForBatchedUpdates();
3132
SequentialQueue.resetQueue();
3233
MainQueue.clear();
3334
HttpUtils.cancelPendingRequests();
3435
NetworkStore.checkRequiredData();
3536
await waitForNetworkPromises();
37+
// Reassign global.fetch to a fresh mock to clear any leftover mockImplementationOnce
38+
// queue from the previous test. jest.clearAllMocks() only resets call counts, not the queue.
39+
global.fetch = TestHelper.getGlobalFetchMock();
3640
jest.clearAllMocks();
3741
Request.clearMiddlewares();
3842
});

0 commit comments

Comments
 (0)