Skip to content

Commit 05e79fb

Browse files
author
Jack Pope
committed
[RTR] Add usage warning behind flag
1 parent f1039be commit 05e79fb

11 files changed

+106
-3
lines changed

packages/react-test-renderer/shallow.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
87
*/
98

10-
export {default} from 'react-shallow-renderer';
9+
import ReactShallowRenderer from 'react-shallow-renderer';
10+
import {enableReactTestRendererWarning} from 'shared/ReactFeatureFlags';
11+
12+
const emptyObject = {};
13+
14+
export default class ReactShallowRendererWithWarning extends ReactShallowRenderer {
15+
render(element, context = emptyObject) {
16+
if (__DEV__) {
17+
if (enableReactTestRendererWarning === true) {
18+
console.warn(
19+
"React's Shallow Renderer export will be removed in a future release. " +
20+
'Please use @testing-library/react instead.',
21+
);
22+
}
23+
}
24+
25+
return super.render(element, context);
26+
}
27+
}

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

Lines changed: 13 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.unstable_act;
5861

@@ -471,6 +474,15 @@ 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+
'Support for ReactTestRenderer will be removed in a future release. ' +
481+
'Please use @testing-library/react instead.',
482+
);
483+
}
484+
}
485+
474486
let createNodeMock = defaultTestOptions.createNodeMock;
475487
let isConcurrent = false;
476488
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: Support for ReactTestRenderer will be removed in a future release. Please use @testing-library/react instead.',
65+
{withoutStack: true},
66+
);
67+
});
68+
5469
it('renders a simple component', () => {
5570
function Link() {
5671
return <a role="link" />;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails react-core
8+
*/
9+
10+
'use strict';
11+
12+
let React;
13+
let ReactShallowRenderer;
14+
let ReactFeatureFlags;
15+
16+
function HelloWorld() {
17+
return <h1>Hello, world!</h1>;
18+
}
19+
20+
describe('ShallowRenderer', () => {
21+
beforeEach(() => {
22+
jest.resetModules();
23+
ReactFeatureFlags = require('shared/ReactFeatureFlags');
24+
React = require('react');
25+
ReactShallowRenderer = require('../../shallow.js').default;
26+
});
27+
28+
it('should render without warnings without enableReactTestRendererWarning', () => {
29+
ReactFeatureFlags.enableReactTestRendererWarning = false;
30+
const renderer = new ReactShallowRenderer();
31+
expect(renderer.render(<HelloWorld />)).toMatchSnapshot();
32+
});
33+
34+
it('should render with warnings with enableReactTestRendererWarning', () => {
35+
ReactFeatureFlags.enableReactTestRendererWarning = true;
36+
const renderer = new ReactShallowRenderer();
37+
expect(() => {
38+
renderer.render(<HelloWorld />);
39+
}).toWarnDev(
40+
"Warning: React's Shallow Renderer export will be removed in a future release. Please use @testing-library/react instead.",
41+
{withoutStack: true},
42+
);
43+
});
44+
});

packages/shared/ReactFeatureFlags.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ export const enableUnifiedSyncLane = true;
162162
// Adds an opt-in to time slicing for updates that aren't wrapped in startTransition.
163163
export const allowConcurrentByDefault = false;
164164

165+
// Warn on any usage of ReactTestRenderer
166+
export const enableReactTestRendererWarning = false;
167+
165168
// -----------------------------------------------------------------------------
166169
// React DOM Chopping Block
167170
//

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,7 @@ export const enableFizzExternalRuntime = false;
9696
export const enableAsyncActions = false;
9797
export const enableUseDeferredValueInitialArg = true;
9898

99+
export const enableReactTestRendererWarning = false;
100+
99101
// Flow magic to verify the exports of this file match the original version.
100102
((((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
@@ -87,5 +87,7 @@ export const useMicrotasksForSchedulingInFabric = false;
8787
export const passChildrenWhenCloningPersistedNodes = false;
8888
export const enableUseDeferredValueInitialArg = __EXPERIMENTAL__;
8989

90+
export const enableReactTestRendererWarning = false;
91+
9092
// Flow magic to verify the exports of this file match the original version.
9193
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,7 @@ export const useMicrotasksForSchedulingInFabric = false;
8787
export const passChildrenWhenCloningPersistedNodes = false;
8888
export const enableUseDeferredValueInitialArg = __EXPERIMENTAL__;
8989

90+
export const enableReactTestRendererWarning = false;
91+
9092
// Flow magic to verify the exports of this file match the original version.
9193
((((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
@@ -84,5 +84,7 @@ export const useMicrotasksForSchedulingInFabric = false;
8484
export const passChildrenWhenCloningPersistedNodes = false;
8585
export const enableUseDeferredValueInitialArg = __EXPERIMENTAL__;
8686

87+
export const enableReactTestRendererWarning = false;
88+
8789
// Flow magic to verify the exports of this file match the original version.
8890
((((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
@@ -87,5 +87,7 @@ export const useMicrotasksForSchedulingInFabric = false;
8787
export const passChildrenWhenCloningPersistedNodes = false;
8888
export const enableUseDeferredValueInitialArg = true;
8989

90+
export const enableReactTestRendererWarning = false;
91+
9092
// Flow magic to verify the exports of this file match the original version.
9193
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

0 commit comments

Comments
 (0)