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 e325338
Show file tree
Hide file tree
Showing 12 changed files with 107 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);
}
}
11 changes: 10 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,7 @@ 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 +471,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 @@ -14,6 +14,7 @@ let React;
let ReactCache;
let ReactTestRenderer;
let waitForAll;
let ReactFeatureFlags;

describe('ReactTestRenderer', () => {
beforeEach(() => {
Expand All @@ -25,10 +26,21 @@ describe('ReactTestRenderer', () => {
React = require('react');
ReactCache = require('react-cache');
ReactTestRenderer = require('react-test-renderer');
ReactFeatureFlags = require('shared/ReactFeatureFlags');
const InternalTestUtils = require('internal-test-utils');
waitForAll = InternalTestUtils.waitForAll;
});

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('should warn if used to render a ReactDOM portal', () => {
const container = document.createElement('div');
expect(() => {
Expand Down
44 changes: 44 additions & 0 deletions packages/react-test-renderer/src/__tests__/ShallowRenderer-test.js
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},
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ShallowRenderer should render without warnings without enableReactTestRendererWarning 1`] = `
<h1>
Hello, world!
</h1>
`;
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 e325338

Please sign in to comment.