mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Deprecate act from react-dom/test-utils in favor of act from react (#28597)
This commit is contained in:
@@ -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},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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
@@ -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) {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user