From 664f7c0dcfcd7ada8e235daeb1e0e9015ff4e3fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Tue, 5 Aug 2025 07:06:24 -0700 Subject: [PATCH] Add test to verify that components like View and Text are not loaded as a side-effect of environment initialization (#53053) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/53053 Changelog: [internal] This adds a Fantom test to ensure that setting up the RN environment doesn't trigger the initialization for React components like View and Text, which should be lazy loaded when necessary. Reviewed By: rshest Differential Revision: D79636160 fbshipit-source-id: ef1fbd6cd531eb7082dce000ba74a5eed451e259 --- ...etUpDefaultReactNativeEnvironment-itest.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 packages/react-native/src/private/setup/__tests__/setUpDefaultReactNativeEnvironment-itest.js diff --git a/packages/react-native/src/private/setup/__tests__/setUpDefaultReactNativeEnvironment-itest.js b/packages/react-native/src/private/setup/__tests__/setUpDefaultReactNativeEnvironment-itest.js new file mode 100644 index 00000000000..297be14e172 --- /dev/null +++ b/packages/react-native/src/private/setup/__tests__/setUpDefaultReactNativeEnvironment-itest.js @@ -0,0 +1,56 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +import setUpDefaultReactNativeEnvironment from 'react-native/src/private/setup/setUpDefaultReactNativeEnvironment'; + +describe('setUpReactNativeEnvironment', () => { + it('should not load components as a side effect', () => { + // We set up has not been done yet. + expect(globalThis.self).toBeUndefined(); + + setUpDefaultReactNativeEnvironment(false); + + // The set up worked. + expect(globalThis.self).toBe(globalThis); + + // Verify that `View` and `Text` are not loaded as a side-effect + + // $FlowExpectedError[prop-missing] + const viewModuleId = require.resolveWeak( + 'react-native/Libraries/Components/View/View', + ); + // $FlowExpectedError[prop-missing] + const textModuleId = require.resolveWeak( + 'react-native/Libraries/Text/Text', + ); + // $FlowExpectedError[prop-missing] + const metroModules = require.getModules(); + const viewModule = metroModules.get(viewModuleId); + const textModule = metroModules.get(textModuleId); + expect(viewModule).toBeInstanceOf(Object); + expect(textModule).toBeInstanceOf(Object); + + expect(viewModule.isInitialized).toBe(false); + expect(textModule.isInitialized).toBe(false); + + // But then we can detect when they're loaded (to verify the previous + // detection works). + + require('react-native').View; + + expect(viewModule.isInitialized).toBe(true); + expect(textModule.isInitialized).toBe(false); + + require('react-native').Text; + + expect(viewModule.isInitialized).toBe(true); + expect(textModule.isInitialized).toBe(true); + }); +});