77 * @flow
88 */
99
10+ import semver from 'semver' ;
11+
1012import typeof ReactTestRenderer from 'react-test-renderer' ;
1113
1214import type { FrontendBridge } from 'react-devtools-shared/src/bridge' ;
1315import type Store from 'react-devtools-shared/src/devtools/store' ;
1416import type { ProfilingDataFrontend } from 'react-devtools-shared/src/devtools/views/Profiler/types' ;
1517import type { ElementType } from 'react-devtools-shared/src/frontend/types' ;
1618
19+ import { ReactVersion } from '../../../../ReactVersions' ;
20+
21+ const requestedReactVersion = process . env . REACT_VERSION || ReactVersion ;
22+ export function getActDOMImplementation ( ) : ( ) => void | Promise < void > {
23+ // This is for React < 18, where act was distributed in react-dom/test-utils.
24+ if ( semver . lt ( requestedReactVersion , '18.0.0' ) ) {
25+ const ReactDOMTestUtils = require ( 'react-dom/test-utils' ) ;
26+ return ReactDOMTestUtils . act ;
27+ }
28+
29+ const React = require ( 'react' ) ;
30+ // This is for React 18, where act was distributed in react as unstable.
31+ if ( React . unstable_act ) {
32+ return React . unstable_act ;
33+ }
34+
35+ // This is for React > 18, where act is marked as stable.
36+ if ( React . act ) {
37+ return React . act ;
38+ }
39+
40+ throw new Error ( "Couldn't find any available act implementation" ) ;
41+ }
42+
1743export function act (
1844 callback : Function ,
1945 recursivelyFlush : boolean = true ,
2046) : void {
47+ // act from react-test-renderer has some side effects on React DevTools
48+ // it injects the renderer for DevTools, see ReactTestRenderer.js
2149 const { act : actTestRenderer } = require ( 'react-test-renderer' ) ;
22- // Use `require('react-dom/test-utils').act` as a fallback for React 17, which can be used in integration tests for React DevTools.
23- const actDOM =
24- require ( 'react' ) . act ||
25- require ( 'react' ) . unstable_act ||
26- require ( 'react-dom/test-utils' ) . act ;
50+ const actDOM = getActDOMImplementation ( ) ;
2751
2852 actDOM ( ( ) => {
2953 actTestRenderer ( ( ) => {
@@ -47,10 +71,10 @@ export async function actAsync(
4771 cb : ( ) = > * ,
4872 recursivelyFlush : boolean = true ,
4973) : Promise < void > {
74+ // act from react-test-renderer has some side effects on React DevTools
75+ // it injects the renderer for DevTools, see ReactTestRenderer.js
5076 const { act : actTestRenderer } = require ( 'react-test-renderer' ) ;
51- // Use `require('react-dom/test-utils').act` as a fallback for React 17, which can be used in integration tests for React DevTools.
52- const actDOM =
53- require ( 'react' ) . unstable_act || require ( 'react-dom/test-utils' ) . act ;
77+ const actDOM = getActDOMImplementation ( ) ;
5478
5579 await actDOM ( async ( ) => {
5680 await actTestRenderer ( async ( ) => {
0 commit comments