@samitouri / QOS-React-1 / commits / 66c8346401

[RTR] Add usage warning behind flag (#27903)

## Summary Moving towards deprecation of ReactTestRenderer. Log a warning on each render so we can remove the exports in a future major version. We can enable this flag in web RTR without disrupting RN tests by flipping the flag in `packages/shared/forks/ReactFeatureFlags.test-renderer.js` ## How did you test this change? `yarn test packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.js`

Jack Pope committed Feb 23, 2024 at 11:33 UTC 66c8346401d271588e4c400921c5dab5478fc623
9 files changed +43 -1
packages/react-test-renderer/src/ReactTestRenderer.js
+12 -1
@@ -52,7 +52,10 @@ import {checkPropStringCoercion} from 'shared/CheckStringCoercion';
52
53 import {getPublicInstance} from './ReactFiberConfigTestHost';
54 import {ConcurrentRoot, LegacyRoot} from 'react-reconciler/src/ReactRootTags';
55 -import {allowConcurrentByDefault} from 'shared/ReactFeatureFlags';
55 +import {
56 + allowConcurrentByDefault,
57 + enableReactTestRendererWarning,
58 +} from 'shared/ReactFeatureFlags';
59
60 const act = React.act;
61
@@ -471,6 +474,14 @@ function create(
474 getInstance(): React$Component<any, any> | PublicInstance | null,
475 unstable_flushSync: typeof flushSync,
476 } {
477 + if (__DEV__) {
478 + if (enableReactTestRendererWarning === true) {
479 + console.warn(
480 + 'react-test-renderer is deprecated. See https://react.dev/warnings/react-test-renderer',
481 + );
482 + }
483 + }
484 +
485 let createNodeMock = defaultTestOptions.createNodeMock;
486 let isConcurrent = false;
487 let isStrictMode = false;
packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js
+15
@@ -51,6 +51,21 @@ function cleanNodeOrArray(node) {
51 }
52
53 describe('ReactTestRenderer', () => {
54 + beforeEach(() => {
55 + jest.resetModules();
56 + ReactFeatureFlags.enableReactTestRendererWarning = false;
57 + });
58 +
59 + it('should warn if enableReactTestRendererWarning is enabled', () => {
60 + ReactFeatureFlags.enableReactTestRendererWarning = true;
61 + expect(() => {
62 + ReactTestRenderer.create(<div />);
63 + }).toWarnDev(
64 + 'Warning: react-test-renderer is deprecated. See https://react.dev/warnings/react-test-renderer',
65 + {withoutStack: true},
66 + );
67 + });
68 +
69 it('renders a simple component', () => {
70 function Link() {
71 return <a role="link" />;
packages/shared/ReactFeatureFlags.js
+5
@@ -187,6 +187,11 @@ export const enableInfiniteRenderLoopDetection = true;
187 // during element creation.
188 export const enableRefAsProp = __NEXT_MAJOR__;
189
190 +// Not ready to break experimental yet.
191 +// Needs more internal cleanup
192 +// Warn on any usage of ReactTestRenderer
193 +export const enableReactTestRendererWarning = false;
194 +
195 // -----------------------------------------------------------------------------
196 // Chopping Block
197 //
packages/shared/forks/ReactFeatureFlags.native-fb.js
+2
@@ -102,5 +102,7 @@ export const enableInfiniteRenderLoopDetection = false;
102 // because JSX is an extremely hot path.
103 export const enableRefAsProp = false;
104
105 +export const enableReactTestRendererWarning = false;
106 +
107 // Flow magic to verify the exports of this file match the original version.
108 ((((null: any): ExportsType): FeatureFlagsType): ExportsType);
packages/shared/forks/ReactFeatureFlags.native-oss.js
+2
@@ -93,5 +93,7 @@ export const enableServerComponentLogs = true;
93 // TODO: Should turn this on in next "major" RN release.
94 export const enableRefAsProp = false;
95
96 +export const enableReactTestRendererWarning = false;
97 +
98 // Flow magic to verify the exports of this file match the original version.
99 ((((null: any): ExportsType): FeatureFlagsType): ExportsType);
packages/shared/forks/ReactFeatureFlags.test-renderer.js
+1
@@ -98,6 +98,7 @@ export const enableInfiniteRenderLoopDetection = false;
98 // flags should be handled by the Fiber config.
99 const __NEXT_MAJOR__ = __EXPERIMENTAL__;
100 export const enableRefAsProp = __NEXT_MAJOR__;
101 +export const enableReactTestRendererWarning = false;
102
103 // Flow magic to verify the exports of this file match the original version.
104 ((((null: any): ExportsType): FeatureFlagsType): ExportsType);
packages/shared/forks/ReactFeatureFlags.test-renderer.native.js
+2
@@ -89,5 +89,7 @@ export const enableServerComponentLogs = true;
89
90 export const enableRefAsProp = false;
91
92 +export const enableReactTestRendererWarning = false;
93 +
94 // Flow magic to verify the exports of this file match the original version.
95 ((((null: any): ExportsType): FeatureFlagsType): ExportsType);
packages/shared/forks/ReactFeatureFlags.test-renderer.www.js
+2
@@ -92,5 +92,7 @@ export const enableInfiniteRenderLoopDetection = false;
92
93 export const enableRefAsProp = false;
94
95 +export const enableReactTestRendererWarning = false;
96 +
97 // Flow magic to verify the exports of this file match the original version.
98 ((((null: any): ExportsType): FeatureFlagsType): ExportsType);
packages/shared/forks/ReactFeatureFlags.www.js
+2
@@ -118,5 +118,7 @@ export const disableClientCache = true;
118 export const enableServerComponentKeys = true;
119 export const enableServerComponentLogs = true;
120
121 +export const enableReactTestRendererWarning = false;
122 +
123 // Flow magic to verify the exports of this file match the original version.
124 ((((null: any): ExportsType): FeatureFlagsType): ExportsType);