Skip to content

Commit

Permalink
Deprecate act from react-dom/test-utils in favor of act from `rea…
Browse files Browse the repository at this point in the history
…ct` (#28597)
  • Loading branch information
eps1lon committed Mar 27, 2024
1 parent ec4d26c commit 2b036d3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
14 changes: 14 additions & 0 deletions packages/react-dom/src/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
);
});
});
6 changes: 2 additions & 4 deletions packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

let React;
let ReactDOMClient;
let ReactTestUtils;
let Scheduler;
let act;
let container;
Expand All @@ -27,7 +26,7 @@ function sleep(period) {
});
}

describe('ReactTestUtils.act()', () => {
describe('React.act()', () => {
afterEach(() => {
jest.restoreAllMocks();
});
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions packages/react-dom/src/test-utils/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand Down

0 comments on commit 2b036d3

Please sign in to comment.