Skip to content

loadBundleFromServer test: remove legacy Jest timers, increased coverage and improved assertions#55863

Closed
chicio wants to merge 8 commits intofacebook:mainfrom
chicio:loadbundlefromserver_jest_timers_and_improvements
Closed

loadBundleFromServer test: remove legacy Jest timers, increased coverage and improved assertions#55863
chicio wants to merge 8 commits intofacebook:mainfrom
chicio:loadbundlefromserver_jest_timers_and_improvements

Conversation

@chicio
Copy link
Copy Markdown
Contributor

@chicio chicio commented Mar 3, 2026

Summary:

This PR removes the dependency on Jest legacy fake timers from the loadBundleFromServer test suite.

Together with #55757, this should remove the last usages of jest.useFakeTimers({legacyFakeTimers: true}); in the codebase.

In this PR in particular, instead of migrating to modern timers, I refactored the addListener mock to use a Promise based approach. By leveraging microtask scheduling, the tests simulate asynchronous functions flushed after the execution of the sendRequest without relying on timers.

In addition, I also improved the tests in other areas:

  • improved readability by extracting a constant for the request id, used in both sendRequest and addListener mocks
  • improved errors assertions to explicitly verify the errors are thrown (before the refactoring these tests could still be green if no error was thrown, because the expectations were only in the catch clause that could be skipped)
  • improved the tests that verify the successful bundle load:
    • spit long test in ad hoc isolated ones
    • added listeners registration and cleanup verification to improve the coverage
    • added more fine-grained check for the sendRequest parameters

I tried to separate all the changes above in specific commit so that the review can check the incremental improvements I applied with the refactoring steps.

Changelog:

[GENERAL] [CHANGED] - loadBundleFromServer test: remove legacy Jest timers, increased coverage and improved assertions

Test Plan:

  • Ran loadBundleFromServer-test and verified all tests cases passed
  • Ran the React Native test suite to ensure all tests pass
  • Verified test correctness by intentionally breaking production code after fixing the tests (eg. remove observer/change listener code)

chicio added 7 commits March 3, 2026 00:31
Use promise to simulate RCTNetworking delayed callback (microtask flushed at the end of sync code sendRequest)
mocked request id is now shared between sendRequest mock and addListener mock
tests that check load bundle errors now catch then error and check with the expect. This avoid the problem of before implementation: if no error was thrown the tests were still green
Separated tests for checking loading bundle from root or from subdir. Added assertion for removeListener tests.
Tests now check that the addListener and removeListener number of calls matcher. This is testing the invariant that each registered listener should be remove (later on)
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 3, 2026
@chicio chicio marked this pull request as draft March 3, 2026 00:19
@chicio chicio marked this pull request as ready for review March 3, 2026 01:16
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Mar 3, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync bot commented Mar 3, 2026

@vzaidman has imported this pull request. If you are a Meta employee, you can view this in D95034650.

Copy link
Copy Markdown
Contributor

@cortinico cortinico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync bot closed this in a19d98e Mar 3, 2026
@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Mar 3, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync bot commented Mar 3, 2026

@vzaidman merged this pull request in a19d98e.

@react-native-bot
Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @chicio in a19d98e

When will my fix make it into a release? | How to file a pick request?

zoontek pushed a commit to zoontek/react-native that referenced this pull request Mar 9, 2026
…age and improved assertions (facebook#55863)

Summary:
This PR removes the dependency on Jest legacy fake timers from the `loadBundleFromServer` test suite.

Together with facebook#55757, this should remove the last usages of `jest.useFakeTimers({legacyFakeTimers: true});` in the codebase.

In this PR in particular, instead of migrating to modern  timers, I refactored the `addListener` mock to use a Promise based approach. By leveraging microtask scheduling, the tests simulate asynchronous functions flushed after the execution of the `sendRequest` without relying on timers.

In addition, I also improved the tests in other areas:

* improved readability by extracting a constant for the request id, used in both `sendRequest` and `addListener` mocks
* improved errors assertions to explicitly verify the errors are thrown (before the refactoring these tests could still be green if no error was thrown, because the expectations were only in the catch clause that could be skipped)
* improved the tests that verify the successful bundle load:
  * spit long test in ad hoc isolated ones
  * added listeners registration and cleanup verification to improve the coverage
  * added more fine-grained check for the `sendRequest` parameters

I tried to separate all the changes above in specific commit so that the review can check the incremental improvements I applied with the refactoring steps.

## Changelog:

[GENERAL] [CHANGED] - loadBundleFromServer test: remove legacy Jest timers, increased coverage and improved assertions

Pull Request resolved: facebook#55863

Test Plan:
* Ran loadBundleFromServer-test and verified all tests cases passed
* Ran the React Native test suite to ensure all tests pass
* Verified test correctness by intentionally breaking production code after fixing the tests (eg. remove observer/change listener code)

Reviewed By: cortinico

Differential Revision: D95034650

Pulled By: vzaidman

fbshipit-source-id: f6ae7cbc95ccfe606c1054c9044bf733e41bc2f9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants