Skip to content

Commit

Permalink
warn -> error for Test Renderer deprecation (#28904)
Browse files Browse the repository at this point in the history
We use `console.error` for deprecations. `console.warn` is for less
critical issues, like performance anti-patterns.
  • Loading branch information
acdlite committed Apr 24, 2024
1 parent cb15184 commit c516cef
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 42 deletions.
3 changes: 2 additions & 1 deletion packages/internal-test-utils/shouldIgnoreConsoleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = function shouldIgnoreConsoleError(format, args) {
) !== -1 ||
format.indexOf(
'ReactDOM.hydrate has not been supported since React 18',
) !== -1
) !== -1 ||
format.indexOf('react-test-renderer is deprecated.') !== -1
) {
// We haven't finished migrating our tests to use createRoot.
return true;
Expand Down
6 changes: 0 additions & 6 deletions packages/internal-test-utils/shouldIgnoreConsoleWarn.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
'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;
};
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, ⚠ 1
2, ⚠ 0
[root]
<ErrorBoundary> ✕
`);

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

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

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

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

selectNextErrorOrWarning();
expect(state).toMatchInlineSnapshot(`
2, ⚠ 1
3, ⚠ 0
[root]
→ ▾ <ErrorBoundary> ✕
<Child> ✕
Expand Down
1 change: 1 addition & 0 deletions packages/react-devtools-shell/src/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ignoreErrors([
'Warning: Unsafe lifecycle methods',
'Warning: %s is deprecated in StrictMode.', // findDOMNode
'Warning: ReactDOM.render was removed in React 19',
'Warning: react-test-renderer is deprecated',
]);
ignoreWarnings(['Warning: componentWillReceiveProps has been renamed']);
ignoreLogs([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let Scheduler;
let ReactDOMServer;
let act;
let assertLog;
let assertConsoleErrorDev;
let waitForAll;
let waitForThrow;

Expand All @@ -35,6 +36,7 @@ describe('ReactHooks', () => {

const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
assertConsoleErrorDev = InternalTestUtils.assertConsoleErrorDev;
waitForAll = InternalTestUtils.waitForAll;
waitForThrow = InternalTestUtils.waitForThrow;
});
Expand Down Expand Up @@ -1810,7 +1812,6 @@ describe('ReactHooks', () => {
// Regression test for #14674
it('does not swallow original error when updating another component in render phase', async () => {
const {useState} = React;
spyOnDev(console, 'error').mockImplementation(() => {});

let _setState;
function A() {
Expand All @@ -1837,14 +1838,10 @@ describe('ReactHooks', () => {
);
});
}).rejects.toThrow('Hello');

if (__DEV__) {
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error.mock.calls[0][0]).toContain(
'Warning: Cannot update a component (`%s`) while rendering ' +
'a different component (`%s`).',
);
}
assertConsoleErrorDev([
'Warning: Cannot update a component (`A`) while rendering ' +
'a different component (`B`).',
]);
});

// Regression test for https://github.com/facebook/react/issues/15057
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let waitFor;
let waitForAll;
let waitForThrow;
let assertLog;
let assertConsoleErrorDev;
let act;

let fakeModuleCache;
Expand Down Expand Up @@ -34,6 +35,7 @@ describe('ReactLazy', () => {
waitForAll = InternalTestUtils.waitForAll;
waitForThrow = InternalTestUtils.waitForThrow;
assertLog = InternalTestUtils.assertLog;
assertConsoleErrorDev = InternalTestUtils.assertConsoleErrorDev;
act = InternalTestUtils.act;

fakeModuleCache = new Map();
Expand Down Expand Up @@ -205,8 +207,6 @@ describe('ReactLazy', () => {
});

it('does not support arbitrary promises, only module objects', async () => {
spyOnDev(console, 'error').mockImplementation(() => {});

const LazyText = lazy(async () => Text);

const root = ReactTestRenderer.create(null, {
Expand All @@ -228,13 +228,11 @@ describe('ReactLazy', () => {

expect(error.message).toMatch('Element type is invalid');
assertLog(['Loading...']);
assertConsoleErrorDev([
'Expected the result of a dynamic import() call',
'Expected the result of a dynamic import() call',
]);
expect(root).not.toMatchRenderedOutput('Hi');
if (__DEV__) {
expect(console.error).toHaveBeenCalledTimes(2);
expect(console.error.mock.calls[0][0]).toContain(
'Expected the result of a dynamic import() call',
);
}
});

it('throws if promise rejects', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-test-renderer/src/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ function create(
enableReactTestRendererWarning === true &&
global.IS_REACT_NATIVE_TEST_ENVIRONMENT !== true
) {
console.warn(
console.error(
'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 @@ -60,26 +60,25 @@ describe('ReactTestRenderer', () => {
ReactFeatureFlags.enableReactTestRendererWarning = false;
});

// @gate __DEV__
it('should warn if enableReactTestRendererWarning is enabled', () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
ReactFeatureFlags.enableReactTestRendererWarning = true;
expect(() => {
ReactTestRenderer.create(<div />);
}).toWarnDev(
ReactTestRenderer.create(<div />);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error.mock.calls[0][0]).toContain(
'Warning: react-test-renderer is deprecated. See https://react.dev/warnings/react-test-renderer',
{withoutStack: true},
);
console.error.mockRestore();
});

// @gate __DEV__
it('should not warn if enableReactTestRendererWarning is enabled but the RN global is set', () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
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},
);
ReactTestRenderer.create(<div />);
expect(console.error).toHaveBeenCalledTimes(0);
console.error.mockRestore();
});

describe('root tags', () => {
Expand Down

0 comments on commit c516cef

Please sign in to comment.