From 0ead7de5e8016558e91b12c19135ad0cac8a116c Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Tue, 21 Jan 2025 16:43:36 -0800 Subject: [PATCH] VirtualizedList: More Resilient Unit Tests (#48819) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48819 Currently, `VirtualizedList-test.js` has a subtle dependency on how asynchronous operations are queued. Specifically, it depends on... - `Batchinator` to use `setTimeout` for... - `InteractionManager` to use `setImmediate` for... - `InteractionManager` to resolve a promise via microtask. As a consequence, any changes to this queueing logic (e.g. eliminating the unnecessary `setImmediate` and microtask) unnecessarily breaks these unit tests. This changes the Jest unit tests to instead use `jest. advanceTimersToNextTimer()` instead of `jest.runOnlyPendingTimers()` so that the unit tests are no longer dependent on these specific queueing logic. Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D68449850 fbshipit-source-id: 382b1c0a0d8fade873ccf17a9deb3622a83b8163 --- .../Lists/__tests__/VirtualizedList-test.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js index 1c7f7f92dc4..1a4f67dc312 100644 --- a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js +++ b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js @@ -1526,8 +1526,7 @@ it('adjusts render area with non-zero initialScrollIndex', async () => { simulateScroll(component, {x: 0, y: 10}); // simulate scroll offset for initialScrollIndex // TODO: Rewrite test to tolerate subtle timing changes. - performNextBatch(); - performNextBatch(); + jest.advanceTimersToNextTimer(3); }); // We should expand the render area after receiving a message indcating we @@ -2542,5 +2541,5 @@ function performAllBatches() { } function performNextBatch() { - jest.runOnlyPendingTimers(); + jest.advanceTimersToNextTimer(1); }