Skip to content

Commit

Permalink
Move internalAct to internal-test-utils package (#26344)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
acdlite authored Mar 8, 2023
1 parent 8c10062 commit 44d3807
Show file tree
Hide file tree
Showing 83 changed files with 97 additions and 102 deletions.
4 changes: 2 additions & 2 deletions packages/internal-test-utils/ReactInternalTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

// TODO: Move `internalAct` and other test helpers to this package, too

import * as SchedulerMock from 'scheduler/unstable_mock';
import {diff} from 'jest-diff';
import {equals} from '@jest/expect-utils';
import enqueueTask from './enqueueTask';

export {act} from './internalAct';

function assertYieldsWereCleared(Scheduler) {
const actualYields = Scheduler.unstable_clearLog();
if (actualYields.length !== 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
waitForThrow,
assertLog,
} = require('internal-test-utils');
const act = require('jest-react').act;
const act = require('internal-test-utils').act;
const Scheduler = require('scheduler/unstable_mock');

describe('ReactInternalTestUtils', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {Thenable} from 'shared/ReactTypes';

import * as Scheduler from 'scheduler/unstable_mock';

import enqueueTask from 'shared/enqueueTask';
import enqueueTask from './enqueueTask';

let actingUpdatesScopeDepth = 0;

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

const unwind = () => {
if (__DEV__ && actingUpdatesScopeDepth === 1) {
if (actingUpdatesScopeDepth === 1) {
global.IS_REACT_ACT_ENVIRONMENT = previousIsActEnvironment;
}
actingUpdatesScopeDepth--;

if (__DEV__) {
if (actingUpdatesScopeDepth > previousActingUpdatesScopeDepth) {
// if it's _less than_ previousActingUpdatesScopeDepth, then we can
// assume the 'other' one has warned
console.error(
'You seem to have overlapping act() calls, this is not supported. ' +
'Be sure to await previous act() calls before making a new one. ',
);
}
if (actingUpdatesScopeDepth > previousActingUpdatesScopeDepth) {
// if it's _less than_ previousActingUpdatesScopeDepth, then we can
// assume the 'other' one has warned
throw new Error(
'You seem to have overlapping act() calls, this is not supported. ' +
'Be sure to await previous act() calls before making a new one. ',
);
}
};

// TODO: This would be way simpler if we could use async/await. Move this
// function to the internal-test-utils package.
// TODO: This would be way simpler if we used async/await.
try {
const result = scope();
if (
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-react/src/JestReact.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {REACT_ELEMENT_TYPE, REACT_FRAGMENT_TYPE} from 'shared/ReactSymbols';

import isArray from 'shared/isArray';

export {act} from './internalAct';

function captureAssertion(fn) {
// Trick to use a Jest matcher inside another Jest matcher. `fn` contains an
// assertion; if it throws, we capture the error and return it, so the stack
Expand Down
4 changes: 2 additions & 2 deletions packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('ReactFlight', () => {
ReactNoop = require('react-noop-renderer');
ReactNoopFlightServer = require('react-noop-renderer/flight-server');
ReactNoopFlightClient = require('react-noop-renderer/flight-client');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Scheduler = require('scheduler');
const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
Expand Down Expand Up @@ -1184,7 +1184,7 @@ describe('ReactFlight', () => {
ReactNoop = require('react-noop-renderer');
ReactNoopFlightServer = require('react-noop-renderer/flight-server');
ReactNoopFlightClient = require('react-noop-renderer/flight-client');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Scheduler = require('scheduler');

await act(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('ReactHooksInspectionIntegration', () => {
React = require('react');
ReactTestRenderer = require('react-test-renderer');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;
ReactDebugTools = require('react-debug-tools');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ describe('InspectedElement', () => {
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
PropTypes = require('prop-types');
TestUtilsAct = require('jest-react').act;
TestUtilsAct = require('internal-test-utils').act;
TestRenderer = utils.requireTestRenderer();
TestRendererAct = require('jest-react').act;
TestRendererAct = require('internal-test-utils').act;

BridgeContext =
require('react-devtools-shared/src/devtools/views/context').BridgeContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Store component filters', () => {
React = require('react');
Types = require('react-devtools-shared/src/types');
utils = require('./utils');
internalAct = require('jest-react').act;
internalAct = require('internal-test-utils').act;

legacyRender = utils.legacyRender;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('ReactDOMConsoleErrorReporting', () => {

beforeEach(() => {
jest.resetModules();
act = require('jest-react').act;
act = require('internal-test-utils').act;
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('ReactDOMFiberAsync', () => {
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Scheduler = require('scheduler');

const InternalTestUtils = require('internal-test-utils');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('ReactDOMFizzShellHydration', () => {
React = require('react');
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');
clientAct = require('jest-react').act;
clientAct = require('internal-test-utils').act;
ReactDOMFizzServer = require('react-dom/server');
Stream = require('stream');

Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/__tests__/ReactDOMHooks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('ReactDOMHooks', () => {
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;

container = document.createElement('div');
document.body.appendChild(container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;

const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('ReactDOMNestedEvents', () => {
React = require('react');
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;
useState = React.useState;

const InternalTestUtils = require('internal-test-utils');
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/__tests__/ReactDOMRoot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('ReactDOMRoot', () => {
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;
useEffect = React.useEffect;

const InternalTestUtils = require('internal-test-utils');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('ReactDOMSafariMicrotaskBug-test', () => {
container = document.createElement('div');
React = require('react');
ReactDOMClient = require('react-dom/client');
act = require('jest-react').act;
act = require('internal-test-utils').act;

document.body.appendChild(container);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('ReactDOMServerPartialHydration', () => {
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
act = require('jest-react').act;
act = require('internal-test-utils').act;
ReactDOMServer = require('react-dom/server');
Scheduler = require('scheduler');
Suspense = React.Suspense;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('ReactDOMServerSelectiveHydration', () => {
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Scheduler = require('scheduler');
Suspense = React.Suspense;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function initModules() {
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');
act = require('jest-react').act;
act = require('internal-test-utils').act;
if (gate(flags => flags.enableSuspenseList)) {
SuspenseList = React.SuspenseList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('ReactDOMSuspensePlaceholder', () => {
ReactDOM = require('react-dom');
ReactCache = require('react-cache');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Suspense = React.Suspense;
container = document.createElement('div');
document.body.appendChild(container);
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/__tests__/ReactDOMUseId-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('useId', () => {
JSDOM = require('jsdom').JSDOM;
React = require('react');
ReactDOMClient = require('react-dom/client');
clientAct = require('jest-react').act;
clientAct = require('internal-test-utils').act;
ReactDOMFizzServer = require('react-dom/server');
Stream = require('stream');
Suspense = React.Suspense;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('ReactErrorBoundaries', () => {
ReactFeatureFlags.skipUnmountedBoundaries = true;
ReactDOM = require('react-dom');
React = require('react');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Scheduler = require('scheduler');

const InternalTestUtils = require('internal-test-utils');
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/__tests__/ReactUpdates-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('ReactUpdates', () => {
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactTestUtils = require('react-dom/test-utils');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Scheduler = require('scheduler');

const InternalTestUtils = require('internal-test-utils');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ beforeEach(() => {
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;

const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function (initModules) {

function resetModules() {
({ReactDOM, ReactDOMServer} = initModules());
act = require('jest-react').act;
act = require('internal-test-utils').act;
}

function shouldUseDocument(reactElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('DOMPluginEventSystem', () => {
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');
ReactDOMServer = require('react-dom/server');
act = require('jest-react').act;
act = require('internal-test-utils').act;

const InternalTestUtils = require('internal-test-utils');
waitForAll = InternalTestUtils.waitForAll;
Expand Down Expand Up @@ -1279,7 +1279,7 @@ describe('DOMPluginEventSystem', () => {
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');
ReactDOMServer = require('react-dom/server');
act = require('jest-react').act;
act = require('internal-test-utils').act;

const InternalTestUtils = require('internal-test-utils');
waitForAll = InternalTestUtils.waitForAll;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('ChangeEventPlugin', () => {
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Scheduler = require('scheduler');

const InternalTestUtils = require('internal-test-utils');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ describe('SimpleEventPlugin', function () {
assertLog = InternalTestUtils.assertLog;
waitForAll = InternalTestUtils.waitForAll;

act = require('jest-react').act;
act = require('internal-test-utils').act;
});

it('flushes pending interactive work before exiting event handler', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function initializeModules(hasPointerEvents) {
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;

// TODO: This import throws outside of experimental mode. Figure out better
// strategy for gated imports.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('ReactFabric', () => {
require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')
.ReactNativeViewConfigRegistry.register;

act = require('jest-react').act;
act = require('internal-test-utils').act;
});

it('should be able to create and render a native component', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function mockRenderKeys(keyLists) {
const createReactNativeComponentClass =
require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')
.ReactNativeViewConfigRegistry.register;
const {act} = require('jest-react');
const act = require('internal-test-utils').act;

const mockContainerTag = 11;
const MockView = createReactNativeComponentClass('RCTMockView', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Suspense = React.Suspense;
useState = React.useState;

Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/__tests__/ReactCache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('ReactCache', () => {
ReactNoop = require('react-noop-renderer');
Cache = React.unstable_Cache;
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Suspense = React.Suspense;
cache = React.cache;
Offscreen = React.unstable_Offscreen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('ReactClassSetStateCallback', () => {
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;

const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('ReactConcurrentErrorRecovery', () => {
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;
Suspense = React.Suspense;
startTransition = React.startTransition;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('ReactLazyContextPropagation', () => {
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
act = require('jest-react').act;
act = require('internal-test-utils').act;
useState = React.useState;
useContext = React.useContext;
Suspense = React.Suspense;
Expand Down
Loading

0 comments on commit 44d3807

Please sign in to comment.