Skip to content

Commit

Permalink
[RTR] Add usage warning behind flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Pope committed Jan 8, 2024
1 parent f1039be commit 05e79fb
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 3 deletions.
21 changes: 19 additions & 2 deletions packages/react-test-renderer/shallow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export {default} from 'react-shallow-renderer';
import ReactShallowRenderer from 'react-shallow-renderer';
import {enableReactTestRendererWarning} from 'shared/ReactFeatureFlags';

const emptyObject = {};

export default class ReactShallowRendererWithWarning extends ReactShallowRenderer {
render(element, context = emptyObject) {
if (__DEV__) {
if (enableReactTestRendererWarning === true) {
console.warn(
"React's Shallow Renderer export will be removed in a future release. " +
'Please use @testing-library/react instead.',
);
}
}

return super.render(element, context);
}
}
14 changes: 13 additions & 1 deletion packages/react-test-renderer/src/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ import {checkPropStringCoercion} from 'shared/CheckStringCoercion';

import {getPublicInstance} from './ReactFiberConfigTestHost';
import {ConcurrentRoot, LegacyRoot} from 'react-reconciler/src/ReactRootTags';
import {allowConcurrentByDefault} from 'shared/ReactFeatureFlags';
import {
allowConcurrentByDefault,
enableReactTestRendererWarning,
} from 'shared/ReactFeatureFlags';

const act = React.unstable_act;

Expand Down Expand Up @@ -471,6 +474,15 @@ function create(
getInstance(): React$Component<any, any> | PublicInstance | null,
unstable_flushSync: typeof flushSync,
} {
if (__DEV__) {
if (enableReactTestRendererWarning === true) {
console.warn(
'Support for ReactTestRenderer will be removed in a future release. ' +
'Please use @testing-library/react instead.',
);
}
}

let createNodeMock = defaultTestOptions.createNodeMock;
let isConcurrent = false;
let isStrictMode = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ function cleanNodeOrArray(node) {
}

describe('ReactTestRenderer', () => {
beforeEach(() => {
jest.resetModules();
ReactFeatureFlags.enableReactTestRendererWarning = false;
});

it('should warn if enableReactTestRendererWarning is enabled', () => {
ReactFeatureFlags.enableReactTestRendererWarning = true;
expect(() => {
ReactTestRenderer.create(<div />);
}).toWarnDev(
'Warning: Support for ReactTestRenderer will be removed in a future release. Please use @testing-library/react instead.',
{withoutStack: true},
);
});

it('renders a simple component', () => {
function Link() {
return <a role="link" />;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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.
*
* @emails react-core
*/

'use strict';

let React;
let ReactShallowRenderer;
let ReactFeatureFlags;

function HelloWorld() {
return <h1>Hello, world!</h1>;
}

describe('ShallowRenderer', () => {
beforeEach(() => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
React = require('react');
ReactShallowRenderer = require('../../shallow.js').default;
});

it('should render without warnings without enableReactTestRendererWarning', () => {
ReactFeatureFlags.enableReactTestRendererWarning = false;
const renderer = new ReactShallowRenderer();
expect(renderer.render(<HelloWorld />)).toMatchSnapshot();
});

it('should render with warnings with enableReactTestRendererWarning', () => {
ReactFeatureFlags.enableReactTestRendererWarning = true;
const renderer = new ReactShallowRenderer();
expect(() => {
renderer.render(<HelloWorld />);
}).toWarnDev(
"Warning: React's Shallow Renderer export will be removed in a future release. Please use @testing-library/react instead.",
{withoutStack: true},
);
});
});
3 changes: 3 additions & 0 deletions packages/shared/ReactFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ export const enableUnifiedSyncLane = true;
// Adds an opt-in to time slicing for updates that aren't wrapped in startTransition.
export const allowConcurrentByDefault = false;

// Warn on any usage of ReactTestRenderer
export const enableReactTestRendererWarning = false;

// -----------------------------------------------------------------------------
// React DOM Chopping Block
//
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/forks/ReactFeatureFlags.native-fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,7 @@ export const enableFizzExternalRuntime = false;
export const enableAsyncActions = false;
export const enableUseDeferredValueInitialArg = true;

export const enableReactTestRendererWarning = false;

// Flow magic to verify the exports of this file match the original version.
((((null: any): ExportsType): FeatureFlagsType): ExportsType);
2 changes: 2 additions & 0 deletions packages/shared/forks/ReactFeatureFlags.native-oss.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,7 @@ export const useMicrotasksForSchedulingInFabric = false;
export const passChildrenWhenCloningPersistedNodes = false;
export const enableUseDeferredValueInitialArg = __EXPERIMENTAL__;

export const enableReactTestRendererWarning = false;

// Flow magic to verify the exports of this file match the original version.
((((null: any): ExportsType): FeatureFlagsType): ExportsType);
2 changes: 2 additions & 0 deletions packages/shared/forks/ReactFeatureFlags.test-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,7 @@ export const useMicrotasksForSchedulingInFabric = false;
export const passChildrenWhenCloningPersistedNodes = false;
export const enableUseDeferredValueInitialArg = __EXPERIMENTAL__;

export const enableReactTestRendererWarning = false;

// Flow magic to verify the exports of this file match the original version.
((((null: any): ExportsType): FeatureFlagsType): ExportsType);
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,7 @@ export const useMicrotasksForSchedulingInFabric = false;
export const passChildrenWhenCloningPersistedNodes = false;
export const enableUseDeferredValueInitialArg = __EXPERIMENTAL__;

export const enableReactTestRendererWarning = false;

// Flow magic to verify the exports of this file match the original version.
((((null: any): ExportsType): FeatureFlagsType): ExportsType);
2 changes: 2 additions & 0 deletions packages/shared/forks/ReactFeatureFlags.test-renderer.www.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,7 @@ export const useMicrotasksForSchedulingInFabric = false;
export const passChildrenWhenCloningPersistedNodes = false;
export const enableUseDeferredValueInitialArg = true;

export const enableReactTestRendererWarning = false;

// Flow magic to verify the exports of this file match the original version.
((((null: any): ExportsType): FeatureFlagsType): ExportsType);
2 changes: 2 additions & 0 deletions packages/shared/forks/ReactFeatureFlags.www.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,7 @@ export const passChildrenWhenCloningPersistedNodes = false;

export const enableAsyncDebugInfo = false;

export const enableReactTestRendererWarning = false;

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

0 comments on commit 05e79fb

Please sign in to comment.