Skip to content

Commit d4ea75d

Browse files
committed
ReactDOMTestUtils deprecation warnings
Adds a deprecation warning to ReactDOMTestUtils.renderIntoDocument, which is removed in version 19. Also backports the deprecation warning for ReactDOMTestUtils.act.
1 parent 9090712 commit d4ea75d

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

Diff for: packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ let usingPartialRenderer;
1616
const util = require('util');
1717
const realConsoleError = console.error;
1818

19+
const shouldIgnoreConsoleError = require('../../../../scripts/jest/shouldIgnoreConsoleError');
20+
1921
describe('ReactDOMServerHydration', () => {
2022
let container;
2123

@@ -57,6 +59,9 @@ describe('ReactDOMServerHydration', () => {
5759
// We only want console errors in this suite.
5860
return null;
5961
}
62+
if (shouldIgnoreConsoleError(format, ...rest)) {
63+
return null;
64+
}
6065
rest[rest.length - 1] = normalizeCodeLocInfo(rest[rest.length - 1]);
6166
return util.format(format, ...rest);
6267
}

Diff for: packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,11 @@ function runActTests(label, render, unmount, rerender) {
493493
// it's annoying that we have to wait a tick before this warning comes in
494494
await sleep(0);
495495
if (__DEV__) {
496-
expect(console.error.calls.count()).toEqual(1);
496+
expect(console.error.calls.count()).toEqual(2);
497497
expect(console.error.calls.argsFor(0)[0]).toMatch(
498+
'`ReactDOMTestUtils.act` is deprecated ',
499+
);
500+
expect(console.error.calls.argsFor(1)[0]).toMatch(
498501
'You called act(async () => ...) without await.',
499502
);
500503
}
@@ -516,13 +519,16 @@ function runActTests(label, render, unmount, rerender) {
516519

517520
await sleep(150);
518521
if (__DEV__) {
519-
expect(console.error).toHaveBeenCalledTimes(2);
522+
expect(console.error).toHaveBeenCalledTimes(3);
520523
expect(console.error.calls.argsFor(0)[0]).toMatch(
521-
'You seem to have overlapping act() calls',
524+
'`ReactDOMTestUtils.act` is deprecated ',
522525
);
523526
expect(console.error.calls.argsFor(1)[0]).toMatch(
524527
'You seem to have overlapping act() calls',
525528
);
529+
expect(console.error.calls.argsFor(2)[0]).toMatch(
530+
'You seem to have overlapping act() calls',
531+
);
526532
}
527533
});
528534

Diff for: packages/react-dom/src/test-utils/ReactTestUtils.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const getFiberCurrentPropsFromNode = EventInternals[2];
3434
const enqueueStateRestore = EventInternals[3];
3535
const restoreStateIfNeeded = EventInternals[4];
3636

37-
const act = React.unstable_act;
37+
const reactAct = React.unstable_act;
3838

3939
function Event(suffix) {}
4040

@@ -121,7 +121,23 @@ function validateClassInstance(inst, methodName) {
121121
* utilities will suffice for testing purposes.
122122
* @lends ReactTestUtils
123123
*/
124+
125+
let didWarnAboutReactTestUtilsDeprecation = false;
126+
124127
function renderIntoDocument(element) {
128+
if (__DEV__) {
129+
if (!didWarnAboutReactTestUtilsDeprecation) {
130+
didWarnAboutReactTestUtilsDeprecation = true;
131+
console.error(
132+
'ReactDOMTestUtils is deprecated and will be removed in a future ' +
133+
'major release, because it exposes internal implementation details ' +
134+
'that are highly likely to change between releases. Upgrade to a ' +
135+
'modern testing library, such as @testing-library/react. See ' +
136+
'https://react.dev/warnings/react-dom-test-utils for more info.',
137+
);
138+
}
139+
}
140+
125141
const div = document.createElement('div');
126142
// None of our tests actually require attaching the container to the
127143
// DOM, and doing so creates a mess that we rely on test isolation to
@@ -711,6 +727,23 @@ function buildSimulators() {
711727
}
712728
buildSimulators();
713729

730+
let didWarnAboutUsingAct = false;
731+
export const act = __DEV__
732+
? function actWithWarning(callback) {
733+
if (__DEV__) {
734+
if (!didWarnAboutUsingAct) {
735+
didWarnAboutUsingAct = true;
736+
console.error(
737+
'`ReactDOMTestUtils.act` is deprecated in favor of `React.act`. ' +
738+
'Import `act` from `react` instead of `react-dom/test-utils`. ' +
739+
'See https://react.dev/warnings/react-dom-test-utils for more info.',
740+
);
741+
}
742+
}
743+
return reactAct(callback);
744+
}
745+
: reactAct;
746+
714747
export {
715748
renderIntoDocument,
716749
isElement,
@@ -729,5 +762,4 @@ export {
729762
mockComponent,
730763
nativeTouchData,
731764
Simulate,
732-
act,
733765
};

Diff for: scripts/jest/shouldIgnoreConsoleError.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ module.exports = function shouldIgnoreConsoleError(format, args) {
2929
) !== -1 ||
3030
format.indexOf(
3131
'uses the legacy childContextTypes API which is no longer supported and will be removed'
32-
) !== -1
32+
) !== -1 ||
33+
format.indexOf('ReactDOMTestUtils is deprecated') !== -1 ||
34+
format.indexOf('`ReactDOMTestUtils.act` is deprecated') !== -1
3335
) {
3436
// This is a backported warning. In `main`, there's a different warning
3537
// (and it's fully tested). Not going to bother upgrading all the tests

0 commit comments

Comments
 (0)