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
This commit is contained in:
Rubén Norte
2025-08-05 07:06:24 -07:00
committed by Facebook GitHub Bot
parent 14c869cee6
commit 664f7c0dcf
@@ -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);
});
});