7
7
* @flow
8
8
*/
9
9
10
+ import semver from 'semver' ;
11
+
10
12
import typeof ReactTestRenderer from 'react-test-renderer' ;
11
13
12
14
import type { FrontendBridge } from 'react-devtools-shared/src/bridge' ;
13
15
import type Store from 'react-devtools-shared/src/devtools/store' ;
14
16
import type { ProfilingDataFrontend } from 'react-devtools-shared/src/devtools/views/Profiler/types' ;
15
17
import type { ElementType } from 'react-devtools-shared/src/frontend/types' ;
16
18
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
+
17
43
export function act (
18
44
callback : Function ,
19
45
recursivelyFlush : boolean = true ,
20
46
) : void {
47
+ // act from react-test-renderer has some side effects on React DevTools
48
+ // it injects the renderer for DevTools, see ReactTestRenderer.js
21
49
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 ( ) ;
27
51
28
52
actDOM ( ( ) => {
29
53
actTestRenderer ( ( ) => {
@@ -47,10 +71,10 @@ export async function actAsync(
47
71
cb : ( ) = > * ,
48
72
recursivelyFlush : boolean = true ,
49
73
) : 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
50
76
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 ( ) ;
54
78
55
79
await actDOM ( async ( ) => {
56
80
await actTestRenderer ( async ( ) => {
0 commit comments