Skip to content

Commit 2803430

Browse files
committed
Deprecate act from react-dom/test-utils in favor of act from react
1 parent f5c3394 commit 2803430

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,17 @@ describe('ReactTestUtils', () => {
548548
'For testing React, we recommend React Testing Library instead: https://testing-library.com/docs/react-testing-library/intro.',
549549
);
550550
});
551+
552+
// @gate __DEV__
553+
it('warns when using `act`', () => {
554+
expect(() => {
555+
ReactTestUtils.act(() => {});
556+
}).toErrorDev(
557+
[
558+
'`ReactDOMTestUtils.act` is deprecated in favor of `React.act`. ' +
559+
'Import `act` from `react` instead of `react-dom/test-utils`.',
560+
],
561+
{withoutStack: true},
562+
);
563+
});
551564
});

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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 11 additions & 3 deletions
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(scope) {
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(scope);
48+
}
4149

4250
function Event(suffix) {}
4351

0 commit comments

Comments
 (0)