Skip to content

Commit 66c8346

Browse files
authored
[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`
1 parent 98b8359 commit 66c8346

9 files changed

+43
-1
lines changed

packages/react-test-renderer/src/ReactTestRenderer.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ import {checkPropStringCoercion} from 'shared/CheckStringCoercion';
5252

5353
import {getPublicInstance} from './ReactFiberConfigTestHost';
5454
import {ConcurrentRoot, LegacyRoot} from 'react-reconciler/src/ReactRootTags';
55-
import {allowConcurrentByDefault} from 'shared/ReactFeatureFlags';
55+
import {
56+
allowConcurrentByDefault,
57+
enableReactTestRendererWarning,
58+
} from 'shared/ReactFeatureFlags';
5659

5760
const act = React.act;
5861

@@ -471,6 +474,14 @@ function create(
471474
getInstance(): React$Component<any, any> | PublicInstance | null,
472475
unstable_flushSync: typeof flushSync,
473476
} {
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+
474485
let createNodeMock = defaultTestOptions.createNodeMock;
475486
let isConcurrent = false;
476487
let isStrictMode = false;

packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ function cleanNodeOrArray(node) {
5151
}
5252

5353
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+
5469
it('renders a simple component', () => {
5570
function Link() {
5671
return <a role="link" />;

packages/shared/ReactFeatureFlags.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ export const enableInfiniteRenderLoopDetection = true;
187187
// during element creation.
188188
export const enableRefAsProp = __NEXT_MAJOR__;
189189

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+
190195
// -----------------------------------------------------------------------------
191196
// Chopping Block
192197
//

packages/shared/forks/ReactFeatureFlags.native-fb.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,7 @@ export const enableInfiniteRenderLoopDetection = false;
102102
// because JSX is an extremely hot path.
103103
export const enableRefAsProp = false;
104104

105+
export const enableReactTestRendererWarning = false;
106+
105107
// Flow magic to verify the exports of this file match the original version.
106108
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

packages/shared/forks/ReactFeatureFlags.native-oss.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,7 @@ export const enableServerComponentLogs = true;
9393
// TODO: Should turn this on in next "major" RN release.
9494
export const enableRefAsProp = false;
9595

96+
export const enableReactTestRendererWarning = false;
97+
9698
// Flow magic to verify the exports of this file match the original version.
9799
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

packages/shared/forks/ReactFeatureFlags.test-renderer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export const enableInfiniteRenderLoopDetection = false;
9898
// flags should be handled by the Fiber config.
9999
const __NEXT_MAJOR__ = __EXPERIMENTAL__;
100100
export const enableRefAsProp = __NEXT_MAJOR__;
101+
export const enableReactTestRendererWarning = false;
101102

102103
// Flow magic to verify the exports of this file match the original version.
103104
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

packages/shared/forks/ReactFeatureFlags.test-renderer.native.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,7 @@ export const enableServerComponentLogs = true;
8989

9090
export const enableRefAsProp = false;
9191

92+
export const enableReactTestRendererWarning = false;
93+
9294
// Flow magic to verify the exports of this file match the original version.
9395
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

packages/shared/forks/ReactFeatureFlags.test-renderer.www.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,7 @@ export const enableInfiniteRenderLoopDetection = false;
9292

9393
export const enableRefAsProp = false;
9494

95+
export const enableReactTestRendererWarning = false;
96+
9597
// Flow magic to verify the exports of this file match the original version.
9698
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

packages/shared/forks/ReactFeatureFlags.www.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,7 @@ export const disableClientCache = true;
118118
export const enableServerComponentKeys = true;
119119
export const enableServerComponentLogs = true;
120120

121+
export const enableReactTestRendererWarning = false;
122+
121123
// Flow magic to verify the exports of this file match the original version.
122124
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

0 commit comments

Comments
 (0)