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

+2-2
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

+1-1
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 packages/internal-test-utils/internalAct.js

+11-14
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

-2
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

+2-2
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

+1-1
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

+2-2
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

+1-1
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

+1-1
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

+1-1
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');

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('ReactDOMFizzShellHydration', () => {
3232
React = require('react');
3333
ReactDOMClient = require('react-dom/client');
3434
Scheduler = require('scheduler');
35-
clientAct = require('jest-react').act;
35+
clientAct = require('internal-test-utils').act;
3636
ReactDOMFizzServer = require('react-dom/server');
3737
Stream = require('stream');
3838

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('ReactDOMHooks', () => {
2525
ReactDOM = require('react-dom');
2626
ReactDOMClient = require('react-dom/client');
2727
Scheduler = require('scheduler');
28-
act = require('jest-react').act;
28+
act = require('internal-test-utils').act;
2929

3030
container = document.createElement('div');
3131
document.body.appendChild(container);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
2828
ReactDOM = require('react-dom');
2929
ReactDOMClient = require('react-dom/client');
3030
Scheduler = require('scheduler');
31-
act = require('jest-react').act;
31+
act = require('internal-test-utils').act;
3232

3333
const InternalTestUtils = require('internal-test-utils');
3434
assertLog = InternalTestUtils.assertLog;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('ReactDOMNestedEvents', () => {
2222
React = require('react');
2323
ReactDOMClient = require('react-dom/client');
2424
Scheduler = require('scheduler');
25-
act = require('jest-react').act;
25+
act = require('internal-test-utils').act;
2626
useState = React.useState;
2727

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('ReactDOMRoot', () => {
3030
ReactDOMClient = require('react-dom/client');
3131
ReactDOMServer = require('react-dom/server');
3232
Scheduler = require('scheduler');
33-
act = require('jest-react').act;
33+
act = require('internal-test-utils').act;
3434
useEffect = React.useEffect;
3535

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('ReactDOMSafariMicrotaskBug-test', () => {
3939
container = document.createElement('div');
4040
React = require('react');
4141
ReactDOMClient = require('react-dom/client');
42-
act = require('jest-react').act;
42+
act = require('internal-test-utils').act;
4343

4444
document.body.appendChild(container);
4545
});

packages/react-dom/src/__tests__/ReactDOMServerPartialHydration-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('ReactDOMServerPartialHydration', () => {
108108
React = require('react');
109109
ReactDOM = require('react-dom');
110110
ReactDOMClient = require('react-dom/client');
111-
act = require('jest-react').act;
111+
act = require('internal-test-utils').act;
112112
ReactDOMServer = require('react-dom/server');
113113
Scheduler = require('scheduler');
114114
Suspense = React.Suspense;

packages/react-dom/src/__tests__/ReactDOMServerSelectiveHydration-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('ReactDOMServerSelectiveHydration', () => {
137137
ReactDOM = require('react-dom');
138138
ReactDOMClient = require('react-dom/client');
139139
ReactDOMServer = require('react-dom/server');
140-
act = require('jest-react').act;
140+
act = require('internal-test-utils').act;
141141
Scheduler = require('scheduler');
142142
Suspense = React.Suspense;
143143

packages/react-dom/src/__tests__/ReactDOMServerSuspense-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function initModules() {
2828
ReactDOMClient = require('react-dom/client');
2929
ReactDOMServer = require('react-dom/server');
3030
ReactTestUtils = require('react-dom/test-utils');
31-
act = require('jest-react').act;
31+
act = require('internal-test-utils').act;
3232
if (gate(flags => flags.enableSuspenseList)) {
3333
SuspenseList = React.SuspenseList;
3434
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('ReactDOMSuspensePlaceholder', () => {
2626
ReactDOM = require('react-dom');
2727
ReactCache = require('react-cache');
2828
Scheduler = require('scheduler');
29-
act = require('jest-react').act;
29+
act = require('internal-test-utils').act;
3030
Suspense = React.Suspense;
3131
container = document.createElement('div');
3232
document.body.appendChild(container);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('useId', () => {
3131
JSDOM = require('jsdom').JSDOM;
3232
React = require('react');
3333
ReactDOMClient = require('react-dom/client');
34-
clientAct = require('jest-react').act;
34+
clientAct = require('internal-test-utils').act;
3535
ReactDOMFizzServer = require('react-dom/server');
3636
Stream = require('stream');
3737
Suspense = React.Suspense;

packages/react-dom/src/__tests__/ReactErrorBoundaries-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('ReactErrorBoundaries', () => {
4646
ReactFeatureFlags.skipUnmountedBoundaries = true;
4747
ReactDOM = require('react-dom');
4848
React = require('react');
49-
act = require('jest-react').act;
49+
act = require('internal-test-utils').act;
5050
Scheduler = require('scheduler');
5151

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('ReactUpdates', () => {
2626
ReactDOM = require('react-dom');
2727
ReactDOMClient = require('react-dom/client');
2828
ReactTestUtils = require('react-dom/test-utils');
29-
act = require('jest-react').act;
29+
act = require('internal-test-utils').act;
3030
Scheduler = require('scheduler');
3131

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ beforeEach(() => {
2222
React = require('react');
2323
ReactNoop = require('react-noop-renderer');
2424
Scheduler = require('scheduler');
25-
act = require('jest-react').act;
25+
act = require('internal-test-utils').act;
2626

2727
const InternalTestUtils = require('internal-test-utils');
2828
assertLog = InternalTestUtils.assertLog;

packages/react-dom/src/__tests__/utils/ReactDOMServerIntegrationTestUtils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = function (initModules) {
1919

2020
function resetModules() {
2121
({ReactDOM, ReactDOMServer} = initModules());
22-
act = require('jest-react').act;
22+
act = require('internal-test-utils').act;
2323
}
2424

2525
function shouldUseDocument(reactElement) {

packages/react-dom/src/events/__tests__/DOMPluginEventSystem-test.internal.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('DOMPluginEventSystem', () => {
8080
ReactDOMClient = require('react-dom/client');
8181
Scheduler = require('scheduler');
8282
ReactDOMServer = require('react-dom/server');
83-
act = require('jest-react').act;
83+
act = require('internal-test-utils').act;
8484

8585
const InternalTestUtils = require('internal-test-utils');
8686
waitForAll = InternalTestUtils.waitForAll;
@@ -1279,7 +1279,7 @@ describe('DOMPluginEventSystem', () => {
12791279
ReactDOMClient = require('react-dom/client');
12801280
Scheduler = require('scheduler');
12811281
ReactDOMServer = require('react-dom/server');
1282-
act = require('jest-react').act;
1282+
act = require('internal-test-utils').act;
12831283

12841284
const InternalTestUtils = require('internal-test-utils');
12851285
waitForAll = InternalTestUtils.waitForAll;

packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('ChangeEventPlugin', () => {
6060
React = require('react');
6161
ReactDOM = require('react-dom');
6262
ReactDOMClient = require('react-dom/client');
63-
act = require('jest-react').act;
63+
act = require('internal-test-utils').act;
6464
Scheduler = require('scheduler');
6565

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

packages/react-dom/src/events/plugins/__tests__/SimpleEventPlugin-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ describe('SimpleEventPlugin', function () {
250250
assertLog = InternalTestUtils.assertLog;
251251
waitForAll = InternalTestUtils.waitForAll;
252252

253-
act = require('jest-react').act;
253+
act = require('internal-test-utils').act;
254254
});
255255

256256
it('flushes pending interactive work before exiting event handler', async () => {

packages/react-interactions/events/src/dom/create-event-handle/__tests__/useFocusWithin-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function initializeModules(hasPointerEvents) {
2929
ReactDOM = require('react-dom');
3030
ReactDOMClient = require('react-dom/client');
3131
Scheduler = require('scheduler');
32-
act = require('jest-react').act;
32+
act = require('internal-test-utils').act;
3333

3434
// TODO: This import throws outside of experimental mode. Figure out better
3535
// strategy for gated imports.

packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('ReactFabric', () => {
4141
require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')
4242
.ReactNativeViewConfigRegistry.register;
4343

44-
act = require('jest-react').act;
44+
act = require('internal-test-utils').act;
4545
});
4646

4747
it('should be able to create and render a native component', async () => {

packages/react-native-renderer/src/__tests__/ReactFabricHostComponent-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function mockRenderKeys(keyLists) {
3535
const createReactNativeComponentClass =
3636
require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')
3737
.ReactNativeViewConfigRegistry.register;
38-
const {act} = require('jest-react');
38+
const act = require('internal-test-utils').act;
3939

4040
const mockContainerTag = 11;
4141
const MockView = createReactNativeComponentClass('RCTMockView', () => ({

packages/react-reconciler/src/__tests__/ReactCPUSuspense-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
2020
React = require('react');
2121
ReactNoop = require('react-noop-renderer');
2222
Scheduler = require('scheduler');
23-
act = require('jest-react').act;
23+
act = require('internal-test-utils').act;
2424
Suspense = React.Suspense;
2525
useState = React.useState;
2626

packages/react-reconciler/src/__tests__/ReactCache-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('ReactCache', () => {
2424
ReactNoop = require('react-noop-renderer');
2525
Cache = React.unstable_Cache;
2626
Scheduler = require('scheduler');
27-
act = require('jest-react').act;
27+
act = require('internal-test-utils').act;
2828
Suspense = React.Suspense;
2929
cache = React.cache;
3030
Offscreen = React.unstable_Offscreen;

packages/react-reconciler/src/__tests__/ReactClassSetStateCallback-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('ReactClassSetStateCallback', () => {
1111
React = require('react');
1212
ReactNoop = require('react-noop-renderer');
1313
Scheduler = require('scheduler');
14-
act = require('jest-react').act;
14+
act = require('internal-test-utils').act;
1515

1616
const InternalTestUtils = require('internal-test-utils');
1717
assertLog = InternalTestUtils.assertLog;

packages/react-reconciler/src/__tests__/ReactConcurrentErrorRecovery-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('ReactConcurrentErrorRecovery', () => {
1717
React = require('react');
1818
ReactNoop = require('react-noop-renderer');
1919
Scheduler = require('scheduler');
20-
act = require('jest-react').act;
20+
act = require('internal-test-utils').act;
2121
Suspense = React.Suspense;
2222
startTransition = React.startTransition;
2323

packages/react-reconciler/src/__tests__/ReactContextPropagation-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('ReactLazyContextPropagation', () => {
1818
React = require('react');
1919
ReactNoop = require('react-noop-renderer');
2020
Scheduler = require('scheduler');
21-
act = require('jest-react').act;
21+
act = require('internal-test-utils').act;
2222
useState = React.useState;
2323
useContext = React.useContext;
2424
Suspense = React.Suspense;

0 commit comments

Comments
 (0)