Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 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,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},
);
});
});
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()', () => {
eps1lon marked this conversation as resolved.
Show resolved Hide resolved
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
14 changes: 11 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,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) {}

Expand Down