Deprecate act from react-dom/test-utils in favor of act from react (#28597)

This commit is contained in:
Sebastian Silbermann
2024-03-27 16:28:01 +01:00
committed by GitHub
parent ec4d26ceb8
commit 2b036d3f1f
3 changed files with 28 additions and 7 deletions
+14
View File
@@ -632,4 +632,18 @@ describe('ReactTestUtils', () => {
'See https://react.dev/warnings/react-dom-test-utils for more info.',
);
});
// @gate __DEV__
it('warns when using `act`', () => {
expect(() => {
ReactTestUtils.act(() => {});
}).toErrorDev(
[
'`ReactDOMTestUtils.act` is deprecated in favor of `React.act`. ' +
'Import `act` from `react` instead of `react-dom/test-utils`. ' +
'See https://react.dev/warnings/react-dom-test-utils for more info.',
],
{withoutStack: true},
);
});
});
+2 -4
View File
@@ -9,7 +9,6 @@
let React;
let ReactDOMClient;
let ReactTestUtils;
let Scheduler;
let act;
let container;
@@ -27,7 +26,7 @@ function sleep(period) {
});
}
describe('ReactTestUtils.act()', () => {
describe('React.act()', () => {
afterEach(() => {
jest.restoreAllMocks();
});
@@ -84,9 +83,8 @@ function runActTests(render, unmount, rerender) {
jest.resetModules();
React = require('react');
ReactDOMClient = require('react-dom/client');
ReactTestUtils = require('react-dom/test-utils');
Scheduler = require('scheduler');
act = ReactTestUtils.act;
act = React.act;
const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
+12 -3
View File
@@ -35,9 +35,18 @@ const getFiberCurrentPropsFromNode = EventInternals[2];
const enqueueStateRestore = EventInternals[3];
const restoreStateIfNeeded = EventInternals[4];
// TODO: Add a warning if this API is accessed with advice to switch to
// importing directly from the React package instead.
const act = React.act;
let didWarnAboutUsingAct = false;
function act(callback) {
if (didWarnAboutUsingAct === false) {
didWarnAboutUsingAct = true;
console.error(
'`ReactDOMTestUtils.act` is deprecated in favor of `React.act`. ' +
'Import `act` from `react` instead of `react-dom/test-utils`. ' +
'See https://react.dev/warnings/react-dom-test-utils for more info.',
);
}
return React.act(callback);
}
function Event(suffix) {}