Skip to content

Commit

Permalink
Enable RTR create warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Pope committed Mar 26, 2024
1 parent dbfbfb3 commit 6b5e14c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packages/react-devtools-shared/src/__tests__/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ beforeEach(() => {
jest.useFakeTimers();

// Use utils.js#withErrorsOrWarningsIgnored instead of directly mutating this array.
global._ignoredErrorOrWarningMessages = [];
global._ignoredErrorOrWarningMessages = [
'react-test-renderer is deprecated.',
];
function shouldIgnoreConsoleErrorOrWarn(args) {
let firstArg = args[0];
if (
Expand Down
12 changes: 6 additions & 6 deletions packages/react-devtools-shared/src/__tests__/treeContext-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2586,14 +2586,14 @@ describe('TreeListContext', () => {
utils.act(() => TestRenderer.create(<Contexts />));

expect(store).toMatchInlineSnapshot(`
✕ 1, ⚠ 0
✕ 1, ⚠ 1
[root]
<ErrorBoundary> ✕
`);

selectNextErrorOrWarning();
expect(state).toMatchInlineSnapshot(`
✕ 1, ⚠ 0
✕ 1, ⚠ 1
[root]
→ <ErrorBoundary> ✕
`);
Expand Down Expand Up @@ -2648,14 +2648,14 @@ describe('TreeListContext', () => {
utils.act(() => TestRenderer.create(<Contexts />));

expect(store).toMatchInlineSnapshot(`
✕ 1, ⚠ 0
✕ 1, ⚠ 1
[root]
<ErrorBoundary> ✕
`);

selectNextErrorOrWarning();
expect(state).toMatchInlineSnapshot(`
✕ 1, ⚠ 0
✕ 1, ⚠ 1
[root]
→ <ErrorBoundary> ✕
`);
Expand Down Expand Up @@ -2705,15 +2705,15 @@ describe('TreeListContext', () => {
utils.act(() => TestRenderer.create(<Contexts />));

expect(store).toMatchInlineSnapshot(`
✕ 2, ⚠ 0
✕ 2, ⚠ 1
[root]
▾ <ErrorBoundary> ✕
<Child> ✕
`);

selectNextErrorOrWarning();
expect(state).toMatchInlineSnapshot(`
✕ 2, ⚠ 0
✕ 2, ⚠ 1
[root]
→ ▾ <ErrorBoundary> ✕
<Child> ✕
Expand Down
5 changes: 4 additions & 1 deletion packages/react-test-renderer/src/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,10 @@ function create(
unstable_flushSync: typeof flushSync,
} {
if (__DEV__) {
if (enableReactTestRendererWarning === true) {
if (
enableReactTestRendererWarning === true &&
global.IS_REACT_NATIVE_TEST_ENVIRONMENT !== true
) {
console.warn(
'react-test-renderer is deprecated. See https://react.dev/warnings/react-test-renderer',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ describe('ReactTestRenderer', () => {
);
});

// @gate __DEV__
it('should not warn if enableReactTestRendererWarning is enabled but the RN global is set', () => {
global.IS_REACT_NATIVE_TEST_ENVIRONMENT = true;
ReactFeatureFlags.enableReactTestRendererWarning = true;
expect(() => {
ReactTestRenderer.create(<div />);
}).not.toWarnDev(
'Warning: react-test-renderer is deprecated. See https://react.dev/warnings/react-test-renderer',
{withoutStack: true},
);
global.IS_REACT_NATIVE_TEST_ENVIRONMENT = false;
});

it('renders a simple component', () => {
function Link() {
return <a role="link" />;
Expand Down
4 changes: 1 addition & 3 deletions packages/shared/ReactFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,8 @@ export const enableInfiniteRenderLoopDetection = true;
export const enableRefAsProp = __NEXT_MAJOR__;
export const disableStringRefs = __NEXT_MAJOR__;

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

// Disables legacy mode
// This allows us to land breaking changes to remove legacy mode APIs in experimental builds
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/forks/ReactFeatureFlags.test-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ export const enableInfiniteRenderLoopDetection = false;
const __NEXT_MAJOR__ = __EXPERIMENTAL__;
export const enableRefAsProp = __NEXT_MAJOR__;
export const disableStringRefs = __NEXT_MAJOR__;
export const enableReactTestRendererWarning = false;
export const enableBigIntSupport = __NEXT_MAJOR__;
export const disableLegacyMode = __NEXT_MAJOR__;
export const disableLegacyContext = __NEXT_MAJOR__;
export const enableNewBooleanProps = __NEXT_MAJOR__;
export const disableModulePatternComponents = __NEXT_MAJOR__;
export const enableRenderableContext = __NEXT_MAJOR__;
export const enableReactTestRendererWarning = __NEXT_MAJOR__;

// Flow magic to verify the exports of this file match the original version.
((((null: any): ExportsType): FeatureFlagsType): ExportsType);
6 changes: 6 additions & 0 deletions scripts/jest/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const chalk = require('chalk');
const util = require('util');
const shouldIgnoreConsoleError = require('./shouldIgnoreConsoleError');
const shouldIgnoreConsoleWarn = require('./shouldIgnoreConsoleWarn');
const {getTestFlags} = require('./TestFlags');

if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
Expand Down Expand Up @@ -71,6 +72,11 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
return;
}

// Ignore certain React warnings causing test failures
if (methodName === 'warn' && shouldIgnoreConsoleWarn(format)) {
return;
}

// Capture the call stack now so we can warn about it later.
// The call stack has helpful information for the test author.
// Don't throw yet though b'c it might be accidentally caught and suppressed.
Expand Down
11 changes: 11 additions & 0 deletions scripts/jest/shouldIgnoreConsoleWarn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = function shouldIgnoreConsoleWarn(format) {
if (typeof format === 'string') {
if (format.indexOf('Warning: react-test-renderer is deprecated.') === 0) {
return true;
}
}

return false;
};

0 comments on commit 6b5e14c

Please sign in to comment.