Skip to content

Commit e325338

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

12 files changed

+107
-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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ 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 {allowConcurrentByDefault, enableReactTestRendererWarning} from 'shared/ReactFeatureFlags';
5656

5757
const act = React.unstable_act;
5858

@@ -471,6 +471,15 @@ function create(
471471
getInstance(): React$Component<any, any> | PublicInstance | null,
472472
unstable_flushSync: typeof flushSync,
473473
} {
474+
if (__DEV__) {
475+
if (enableReactTestRendererWarning === true) {
476+
console.warn(
477+
'Support for ReactTestRenderer will be removed in a future release. ' +
478+
'Please use @testing-library/react instead.',
479+
);
480+
}
481+
}
482+
474483
let createNodeMock = defaultTestOptions.createNodeMock;
475484
let isConcurrent = false;
476485
let isStrictMode = false;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let React;
1414
let ReactCache;
1515
let ReactTestRenderer;
1616
let waitForAll;
17+
let ReactFeatureFlags;
1718

1819
describe('ReactTestRenderer', () => {
1920
beforeEach(() => {
@@ -25,10 +26,21 @@ describe('ReactTestRenderer', () => {
2526
React = require('react');
2627
ReactCache = require('react-cache');
2728
ReactTestRenderer = require('react-test-renderer');
29+
ReactFeatureFlags = require('shared/ReactFeatureFlags');
2830
const InternalTestUtils = require('internal-test-utils');
2931
waitForAll = InternalTestUtils.waitForAll;
3032
});
3133

34+
it('should warn if enableReactTestRendererWarning is enabled', () => {
35+
ReactFeatureFlags.enableReactTestRendererWarning = true;
36+
expect(() => {
37+
ReactTestRenderer.create(<div />);
38+
}).toWarnDev(
39+
'Warning: Support for ReactTestRenderer will be removed in a future release. Please use @testing-library/react instead.',
40+
{withoutStack: true},
41+
);
42+
});
43+
3244
it('should warn if used to render a ReactDOM portal', () => {
3345
const container = document.createElement('div');
3446
expect(() => {
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+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`ShallowRenderer should render without warnings without enableReactTestRendererWarning 1`] = `
4+
<h1>
5+
Hello, world!
6+
</h1>
7+
`;

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);

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 passChildrenWhenCloningPersistedNodes = false;
118118

119119
export const enableAsyncDebugInfo = false;
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)