Skip to content

Commit d9b56ba

Browse files
committed
Deprecate act from react-dom/test-utils in favor of act from react
1 parent 82e3562 commit d9b56ba

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

packages/react-dom/src/__tests__/ReactTestUtils-test.js

+13
Original file line numberDiff line numberDiff line change
@@ -632,4 +632,17 @@ describe('ReactTestUtils', () => {
632632
'See https://react.dev/warnings/react-dom-test-utils for more info.',
633633
);
634634
});
635+
636+
// @gate __DEV__
637+
it('warns when using `act`', () => {
638+
expect(() => {
639+
ReactTestUtils.act(() => {});
640+
}).toErrorDev(
641+
[
642+
'`ReactDOMTestUtils.act` is deprecated in favor of `React.act`. ' +
643+
'Import `act` from `react` instead of `react-dom/test-utils`.',
644+
],
645+
{withoutStack: true},
646+
);
647+
});
635648
});

packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
let React;
1111
let ReactDOMClient;
12-
let ReactTestUtils;
1312
let Scheduler;
1413
let act;
1514
let container;
@@ -27,7 +26,7 @@ function sleep(period) {
2726
});
2827
}
2928

30-
describe('ReactTestUtils.act()', () => {
29+
describe('React.act()', () => {
3130
afterEach(() => {
3231
jest.restoreAllMocks();
3332
});
@@ -84,9 +83,8 @@ function runActTests(render, unmount, rerender) {
8483
jest.resetModules();
8584
React = require('react');
8685
ReactDOMClient = require('react-dom/client');
87-
ReactTestUtils = require('react-dom/test-utils');
8886
Scheduler = require('scheduler');
89-
act = ReactTestUtils.act;
87+
act = React.act;
9088

9189
const InternalTestUtils = require('internal-test-utils');
9290
assertLog = InternalTestUtils.assertLog;

packages/react-dom/src/test-utils/ReactTestUtils.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,17 @@ const getFiberCurrentPropsFromNode = EventInternals[2];
3535
const enqueueStateRestore = EventInternals[3];
3636
const restoreStateIfNeeded = EventInternals[4];
3737

38-
// TODO: Add a warning if this API is accessed with advice to switch to
39-
// importing directly from the React package instead.
40-
const act = React.act;
38+
let didWarnAboutUsingAct = false;
39+
function act(callback) {
40+
if (didWarnAboutUsingAct === false) {
41+
didWarnAboutUsingAct = true;
42+
console.error(
43+
'`ReactDOMTestUtils.act` is deprecated in favor of `React.act`. ' +
44+
'Import `act` from `react` instead of `react-dom/test-utils`.',
45+
);
46+
}
47+
return React.act(callback);
48+
}
4149

4250
function Event(suffix) {}
4351

0 commit comments

Comments
 (0)