From 32549cf791e2cc6199860d5ee06c4134f39957ed Mon Sep 17 00:00:00 2001 From: eps1lon Date: Wed, 20 Mar 2024 19:14:53 +0100 Subject: [PATCH 1/2] Deprecate `act` from `react-dom/test-utils` in favor of `act` from `react` --- .../react-dom/src/__tests__/ReactTestUtils-test.js | 13 +++++++++++++ .../src/__tests__/ReactTestUtilsAct-test.js | 6 ++---- .../react-dom/src/test-utils/ReactTestUtils.js | 14 +++++++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactTestUtils-test.js b/packages/react-dom/src/__tests__/ReactTestUtils-test.js index 6fe2ce4e2f753..b28b49deb0aa0 100644 --- a/packages/react-dom/src/__tests__/ReactTestUtils-test.js +++ b/packages/react-dom/src/__tests__/ReactTestUtils-test.js @@ -632,4 +632,17 @@ 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`.', + ], + {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 823cf8e46a696..e4c0dee8f1e74 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(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`.', + ); + } + return React.act(callback); +} function Event(suffix) {} From d8733fa2a7c9b9b140b5b6d1f66aa05423d5cda7 Mon Sep 17 00:00:00 2001 From: eps1lon Date: Wed, 27 Mar 2024 16:15:22 +0100 Subject: [PATCH 2/2] Also link warning page to make it cohesive --- packages/react-dom/src/__tests__/ReactTestUtils-test.js | 3 ++- packages/react-dom/src/test-utils/ReactTestUtils.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactTestUtils-test.js b/packages/react-dom/src/__tests__/ReactTestUtils-test.js index b28b49deb0aa0..e3ed7c71a6883 100644 --- a/packages/react-dom/src/__tests__/ReactTestUtils-test.js +++ b/packages/react-dom/src/__tests__/ReactTestUtils-test.js @@ -640,7 +640,8 @@ describe('ReactTestUtils', () => { }).toErrorDev( [ '`ReactDOMTestUtils.act` is deprecated in favor of `React.act`. ' + - 'Import `act` from `react` instead of `react-dom/test-utils`.', + 'Import `act` from `react` instead of `react-dom/test-utils`. ' + + 'See https://react.dev/warnings/react-dom-test-utils for more info.', ], {withoutStack: true}, ); diff --git a/packages/react-dom/src/test-utils/ReactTestUtils.js b/packages/react-dom/src/test-utils/ReactTestUtils.js index e4c0dee8f1e74..1b4125c819e61 100644 --- a/packages/react-dom/src/test-utils/ReactTestUtils.js +++ b/packages/react-dom/src/test-utils/ReactTestUtils.js @@ -41,7 +41,8 @@ function act(callback) { didWarnAboutUsingAct = true; console.error( '`ReactDOMTestUtils.act` is deprecated in favor of `React.act`. ' + - 'Import `act` from `react` instead of `react-dom/test-utils`.', + '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);