Skip to content

Commit 44d3807

Browse files
authored
Move internalAct to internal-test-utils package (#26344)
This is not a public API. We only use it for our internal tests, the ones in this repo. Let's move it to this private package. Practically speaking this will also let us use async/await in the implementation.
1 parent 8c10062 commit 44d3807

File tree

83 files changed

+97
-102
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+97
-102
lines changed

packages/internal-test-utils/ReactInternalTestUtils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// TODO: Move `internalAct` and other test helpers to this package, too
9-
108
import * as SchedulerMock from 'scheduler/unstable_mock';
119
import {diff} from 'jest-diff';
1210
import {equals} from '@jest/expect-utils';
1311
import enqueueTask from './enqueueTask';
1412

13+
export {act} from './internalAct';
14+
1515
function assertYieldsWereCleared(Scheduler) {
1616
const actualYields = Scheduler.unstable_clearLog();
1717
if (actualYields.length !== 0) {

packages/internal-test-utils/__tests__/ReactInternalTestUtils-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const {
2020
waitForThrow,
2121
assertLog,
2222
} = require('internal-test-utils');
23-
const act = require('jest-react').act;
23+
const act = require('internal-test-utils').act;
2424
const Scheduler = require('scheduler/unstable_mock');
2525

2626
describe('ReactInternalTestUtils', () => {

packages/jest-react/src/internalAct.js renamed to packages/internal-test-utils/internalAct.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {Thenable} from 'shared/ReactTypes';
1818

1919
import * as Scheduler from 'scheduler/unstable_mock';
2020

21-
import enqueueTask from 'shared/enqueueTask';
21+
import enqueueTask from './enqueueTask';
2222

2323
let actingUpdatesScopeDepth = 0;
2424

@@ -40,32 +40,29 @@ export function act<T>(scope: () => Thenable<T>): Thenable<T> {
4040
const previousIsActEnvironment = global.IS_REACT_ACT_ENVIRONMENT;
4141
const previousActingUpdatesScopeDepth = actingUpdatesScopeDepth;
4242
actingUpdatesScopeDepth++;
43-
if (__DEV__ && actingUpdatesScopeDepth === 1) {
43+
if (actingUpdatesScopeDepth === 1) {
4444
// Because this is not the "real" `act`, we set this to `false` so React
4545
// knows not to fire `act` warnings.
4646
global.IS_REACT_ACT_ENVIRONMENT = false;
4747
}
4848

4949
const unwind = () => {
50-
if (__DEV__ && actingUpdatesScopeDepth === 1) {
50+
if (actingUpdatesScopeDepth === 1) {
5151
global.IS_REACT_ACT_ENVIRONMENT = previousIsActEnvironment;
5252
}
5353
actingUpdatesScopeDepth--;
5454

55-
if (__DEV__) {
56-
if (actingUpdatesScopeDepth > previousActingUpdatesScopeDepth) {
57-
// if it's _less than_ previousActingUpdatesScopeDepth, then we can
58-
// assume the 'other' one has warned
59-
console.error(
60-
'You seem to have overlapping act() calls, this is not supported. ' +
61-
'Be sure to await previous act() calls before making a new one. ',
62-
);
63-
}
55+
if (actingUpdatesScopeDepth > previousActingUpdatesScopeDepth) {
56+
// if it's _less than_ previousActingUpdatesScopeDepth, then we can
57+
// assume the 'other' one has warned
58+
throw new Error(
59+
'You seem to have overlapping act() calls, this is not supported. ' +
60+
'Be sure to await previous act() calls before making a new one. ',
61+
);
6462
}
6563
};
6664

67-
// TODO: This would be way simpler if we could use async/await. Move this
68-
// function to the internal-test-utils package.
65+
// TODO: This would be way simpler if we used async/await.
6966
try {
7067
const result = scope();
7168
if (

packages/jest-react/src/JestReact.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import {REACT_ELEMENT_TYPE, REACT_FRAGMENT_TYPE} from 'shared/ReactSymbols';
99

1010
import isArray from 'shared/isArray';
1111

12-
export {act} from './internalAct';
13-
1412
function captureAssertion(fn) {
1513
// Trick to use a Jest matcher inside another Jest matcher. `fn` contains an
1614
// assertion; if it throws, we capture the error and return it, so the stack

packages/react-client/src/__tests__/ReactFlight-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('ReactFlight', () => {
3232
ReactNoop = require('react-noop-renderer');
3333
ReactNoopFlightServer = require('react-noop-renderer/flight-server');
3434
ReactNoopFlightClient = require('react-noop-renderer/flight-client');
35-
act = require('jest-react').act;
35+
act = require('internal-test-utils').act;
3636
Scheduler = require('scheduler');
3737
const InternalTestUtils = require('internal-test-utils');
3838
assertLog = InternalTestUtils.assertLog;
@@ -1184,7 +1184,7 @@ describe('ReactFlight', () => {
11841184
ReactNoop = require('react-noop-renderer');
11851185
ReactNoopFlightServer = require('react-noop-renderer/flight-server');
11861186
ReactNoopFlightClient = require('react-noop-renderer/flight-client');
1187-
act = require('jest-react').act;
1187+
act = require('internal-test-utils').act;
11881188
Scheduler = require('scheduler');
11891189

11901190
await act(async () => {

packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('ReactHooksInspectionIntegration', () => {
2222
React = require('react');
2323
ReactTestRenderer = require('react-test-renderer');
2424
Scheduler = require('scheduler');
25-
act = require('jest-react').act;
25+
act = require('internal-test-utils').act;
2626
ReactDebugTools = require('react-debug-tools');
2727
});
2828

packages/react-devtools-shared/src/__tests__/inspectedElement-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ describe('InspectedElement', () => {
5555
ReactDOM = require('react-dom');
5656
ReactDOMClient = require('react-dom/client');
5757
PropTypes = require('prop-types');
58-
TestUtilsAct = require('jest-react').act;
58+
TestUtilsAct = require('internal-test-utils').act;
5959
TestRenderer = utils.requireTestRenderer();
60-
TestRendererAct = require('jest-react').act;
60+
TestRendererAct = require('internal-test-utils').act;
6161

6262
BridgeContext =
6363
require('react-devtools-shared/src/devtools/views/context').BridgeContext;

packages/react-devtools-shared/src/__tests__/storeComponentFilters-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Store component filters', () => {
3434
React = require('react');
3535
Types = require('react-devtools-shared/src/types');
3636
utils = require('./utils');
37-
internalAct = require('jest-react').act;
37+
internalAct = require('internal-test-utils').act;
3838

3939
legacyRender = utils.legacyRender;
4040
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
2121

2222
beforeEach(() => {
2323
jest.resetModules();
24-
act = require('jest-react').act;
24+
act = require('internal-test-utils').act;
2525
React = require('react');
2626
ReactDOM = require('react-dom');
2727
ReactDOMClient = require('react-dom/client');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('ReactDOMFiberAsync', () => {
3131
React = require('react');
3232
ReactDOM = require('react-dom');
3333
ReactDOMClient = require('react-dom/client');
34-
act = require('jest-react').act;
34+
act = require('internal-test-utils').act;
3535
Scheduler = require('scheduler');
3636

3737
const InternalTestUtils = require('internal-test-utils');

0 commit comments

Comments
 (0)