1
+ // This file and the act() implementation is sourced from react-testing-library
2
+ // https://github.com/testing-library/react-testing-library/blob/c80809a956b0b9f3289c4a6fa8b5e8cc72d6ef6d/src/act-compat.js
1
3
import { act as reactTestRendererAct } from 'react-test-renderer' ;
2
- import { checkReactVersionAtLeast } from './checkReactVersionAtLeast ' ;
4
+ import { checkReactVersionAtLeast } from './react-versions ' ;
3
5
4
6
const actMock = ( callback : ( ) => void ) => {
5
7
callback ( ) ;
6
8
} ;
7
9
8
- type GlobalWithReactActEnvironment = {
9
- IS_REACT_ACT_ENVIRONMENT ?: boolean ;
10
- } & typeof globalThis ;
11
- function getGlobalThis ( ) : GlobalWithReactActEnvironment {
12
- // eslint-disable-next-line no-restricted-globals
13
- if ( typeof self !== 'undefined' ) {
14
- // eslint-disable-next-line no-restricted-globals
15
- return self as GlobalWithReactActEnvironment ;
16
- }
17
- if ( typeof window !== 'undefined' ) {
18
- return window ;
19
- }
20
- if ( typeof global !== 'undefined' ) {
21
- return global ;
22
- }
23
-
24
- throw new Error ( 'unable to locate global object' ) ;
10
+ // See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT
11
+ declare global {
12
+ var IS_REACT_ACT_ENVIRONMENT : boolean | undefined ;
25
13
}
26
14
27
15
function setIsReactActEnvironment ( isReactActEnvironment : boolean | undefined ) {
28
- getGlobalThis ( ) . IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment ;
16
+ globalThis . IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment ;
29
17
}
30
18
31
19
function getIsReactActEnvironment ( ) {
32
- return getGlobalThis ( ) . IS_REACT_ACT_ENVIRONMENT ;
20
+ return globalThis . IS_REACT_ACT_ENVIRONMENT ;
33
21
}
34
22
35
23
type Act = typeof reactTestRendererAct ;
24
+
36
25
function withGlobalActEnvironment ( actImplementation : Act ) {
37
26
return ( callback : Parameters < Act > [ 0 ] ) => {
38
27
const previousActEnvironment = getIsReactActEnvironment ( ) ;
@@ -47,8 +36,9 @@ function withGlobalActEnvironment(actImplementation: Act) {
47
36
if (
48
37
result !== null &&
49
38
typeof result === 'object' &&
39
+ // @ts -expect-error this should be a promise or thenable
50
40
// eslint-disable-next-line promise/prefer-await-to-then
51
- typeof ( result as any ) . then === 'function'
41
+ typeof result . then === 'function'
52
42
) {
53
43
callbackNeedsToBeAwaited = true ;
54
44
}
@@ -87,12 +77,16 @@ function withGlobalActEnvironment(actImplementation: Act) {
87
77
}
88
78
} ;
89
79
}
80
+ const getAct = ( ) => {
81
+ if ( ! reactTestRendererAct ) {
82
+ return actMock ;
83
+ }
90
84
91
- const act = reactTestRendererAct
92
- ? checkReactVersionAtLeast ( 18 , 0 )
85
+ return checkReactVersionAtLeast ( 18 , 0 )
93
86
? withGlobalActEnvironment ( reactTestRendererAct )
94
- : reactTestRendererAct
95
- : actMock ;
87
+ : reactTestRendererAct ;
88
+ } ;
89
+ const act = getAct ( ) ;
96
90
97
91
export default act ;
98
92
export {
0 commit comments