Skip to content

Commit

Permalink
Deprecate act from react-dom/test-utils in favor of act from `r…
Browse files Browse the repository at this point in the history
…eact`
  • Loading branch information
eps1lon committed Mar 20, 2024
1 parent f5c3394 commit 775df84
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
12 changes: 12 additions & 0 deletions packages/react-dom/src/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,16 @@ describe('ReactTestUtils', () => {
'For testing React, we recommend React Testing Library instead: https://testing-library.com/docs/react-testing-library/intro.',
);
});

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()', () => {
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(scope) {
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(scope);
}

function Event(suffix) {}

Expand Down

0 comments on commit 775df84

Please sign in to comment.