Skip to content

Commit

Permalink
[RTR] Add usage warning behind flag (facebook#27903)
Browse files Browse the repository at this point in the history
## 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`
  • Loading branch information
jackpope authored and AndyPengc12 committed Apr 15, 2024
1 parent 34ffeba commit 4e02501
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 1 deletion.
13 changes: 12 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.act;

Expand Down Expand Up @@ -471,6 +474,14 @@ function create(
getInstance(): React$Component<any, any> | PublicInstance | null,
unstable_flushSync: typeof flushSync,
} {
if (__DEV__) {
if (enableReactTestRendererWarning === true) {
console.warn(
'react-test-renderer is deprecated. See https://react.dev/warnings/react-test-renderer',
);
}
}

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: react-test-renderer is deprecated. See https://react.dev/warnings/react-test-renderer',
{withoutStack: true},
);
});

it('renders a simple component', () => {
function Link() {
return <a role="link" />;
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/ReactFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ export const enableInfiniteRenderLoopDetection = true;
// during element creation.
export const enableRefAsProp = __NEXT_MAJOR__;

// Not ready to break experimental yet.
// Needs more internal cleanup
// Warn on any usage of ReactTestRenderer
export const enableReactTestRendererWarning = false;

// -----------------------------------------------------------------------------
// 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 @@ -102,5 +102,7 @@ export const enableInfiniteRenderLoopDetection = false;
// because JSX is an extremely hot path.
export const enableRefAsProp = false;

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 @@ -93,5 +93,7 @@ export const enableServerComponentLogs = true;
// TODO: Should turn this on in next "major" RN release.
export const enableRefAsProp = false;

export const enableReactTestRendererWarning = false;

// Flow magic to verify the exports of this file match the original version.
((((null: any): ExportsType): FeatureFlagsType): ExportsType);
1 change: 1 addition & 0 deletions packages/shared/forks/ReactFeatureFlags.test-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const enableInfiniteRenderLoopDetection = false;
// flags should be handled by the Fiber config.
const __NEXT_MAJOR__ = __EXPERIMENTAL__;
export const enableRefAsProp = __NEXT_MAJOR__;
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 @@ -89,5 +89,7 @@ export const enableServerComponentLogs = true;

export const enableRefAsProp = false;

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 @@ -92,5 +92,7 @@ export const enableInfiniteRenderLoopDetection = false;

export const enableRefAsProp = false;

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 disableClientCache = true;
export const enableServerComponentKeys = true;
export const enableServerComponentLogs = true;

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 4e02501

Please sign in to comment.