1
+ import * as React from 'react' ;
1
2
// This file and the act() implementation is sourced from react-testing-library
2
3
// https://github.com/testing-library/react-testing-library/blob/c80809a956b0b9f3289c4a6fa8b5e8cc72d6ef6d/src/act-compat.js
3
- import { act as reactTestRendererAct } from 'react-test-renderer' ;
4
- import { checkReactVersionAtLeast } from './react-versions' ;
5
4
6
- type ReactAct = typeof reactTestRendererAct ;
5
+ type ReactAct = typeof React . act ;
6
+
7
+ const reactAct = React . act ;
8
+
9
+ function getGlobalThis ( ) {
10
+ /* istanbul ignore else */
11
+ if ( typeof globalThis !== 'undefined' ) {
12
+ return globalThis ;
13
+ }
14
+ /* istanbul ignore next */
15
+ if ( typeof self !== 'undefined' ) {
16
+ return self ;
17
+ }
18
+ /* istanbul ignore next */
19
+ if ( typeof window !== 'undefined' ) {
20
+ return window ;
21
+ }
22
+ /* istanbul ignore next */
23
+ if ( typeof global !== 'undefined' ) {
24
+ return global ;
25
+ }
26
+ /* istanbul ignore next */
27
+ throw new Error ( 'unable to locate global object' ) ;
28
+ }
7
29
8
30
// See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT
9
31
declare global {
10
32
var IS_REACT_ACT_ENVIRONMENT : boolean | undefined ;
11
33
}
12
34
13
35
function setIsReactActEnvironment ( isReactActEnvironment : boolean | undefined ) {
14
- globalThis . IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment ;
36
+ getGlobalThis ( ) . IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment ;
15
37
}
16
38
17
39
function getIsReactActEnvironment ( ) {
18
- return globalThis . IS_REACT_ACT_ENVIRONMENT ;
40
+ return getGlobalThis ( ) . IS_REACT_ACT_ENVIRONMENT ;
19
41
}
20
42
21
43
function withGlobalActEnvironment ( actImplementation : ReactAct ) {
22
44
return ( callback : Parameters < ReactAct > [ 0 ] ) => {
23
45
const previousActEnvironment = getIsReactActEnvironment ( ) ;
24
46
setIsReactActEnvironment ( true ) ;
25
-
26
- // this code is riddled with eslint disabling comments because this doesn't use real promises but eslint thinks we do
27
47
try {
28
48
// The return value of `act` is always a thenable.
29
49
let callbackNeedsToBeAwaited = false ;
30
50
const actResult = actImplementation ( ( ) => {
31
51
const result = callback ( ) ;
32
- if (
33
- result !== null &&
34
- typeof result === 'object' &&
35
- // @ts -expect-error this should be a promise or thenable
36
- // eslint-disable-next-line promise/prefer-await-to-then
37
- typeof result . then === 'function'
38
- ) {
52
+ // @ts -expect-error result is not typed
53
+ if ( result !== null && typeof result === 'object' && typeof result . then === 'function' ) {
39
54
callbackNeedsToBeAwaited = true ;
40
55
}
41
56
return result ;
@@ -44,8 +59,8 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
44
59
if ( callbackNeedsToBeAwaited ) {
45
60
const thenable = actResult ;
46
61
return {
47
- then : ( resolve : ( value : never ) => never , reject : ( value : never ) => never ) => {
48
- // eslint-disable-next-line
62
+ then : ( resolve : ( value : unknown ) => void , reject : ( error : unknown ) => void ) => {
63
+ // eslint-disable-next-line promise/catch-or-return, promise/prefer-await-to-then
49
64
thenable . then (
50
65
// eslint-disable-next-line promise/always-return
51
66
( returnValue ) => {
@@ -72,9 +87,7 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
72
87
} ;
73
88
}
74
89
75
- const act : ReactAct = checkReactVersionAtLeast ( 18 , 0 )
76
- ? ( withGlobalActEnvironment ( reactTestRendererAct ) as ReactAct )
77
- : reactTestRendererAct ;
90
+ const act : ReactAct = withGlobalActEnvironment ( reactAct ) as ReactAct ;
78
91
79
92
export default act ;
80
93
export { setIsReactActEnvironment as setReactActEnvironment , getIsReactActEnvironment } ;
0 commit comments