diff --git a/packages/react-dom/src/__tests__/ReactTestUtils-test.js b/packages/react-dom/src/__tests__/ReactTestUtils-test.js index 3bd2b5d59aa83..fb52229030b21 100644 --- a/packages/react-dom/src/__tests__/ReactTestUtils-test.js +++ b/packages/react-dom/src/__tests__/ReactTestUtils-test.js @@ -548,4 +548,16 @@ describe('ReactTestUtils', () => { 'For testing React, we recommend React Testing Library instead: https://testing-library.com/docs/react-testing-library/intro.', ); }); + + 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`.', + ], + {withoutStack: true}, + ); + }); }); diff --git a/packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js b/packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js index 9f24801907ba1..c76b59ba6e40c 100644 --- a/packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js +++ b/packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js @@ -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; diff --git a/packages/react-dom/src/test-utils/ReactTestUtils.js b/packages/react-dom/src/test-utils/ReactTestUtils.js index da5adc9d20a9b..f7795e6e76d90 100644 --- a/packages/react-dom/src/test-utils/ReactTestUtils.js +++ b/packages/react-dom/src/test-utils/ReactTestUtils.js @@ -35,9 +35,17 @@ 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(scope) { + 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`.', + ); + } + return React.act(scope); +} function Event(suffix) {}