1+ import * as React from 'react' ;
12// This file and the act() implementation is sourced from react-testing-library
23// 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' ;
54
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+ }
729
830// See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT
931declare global {
1032 var IS_REACT_ACT_ENVIRONMENT : boolean | undefined ;
1133}
1234
1335function setIsReactActEnvironment ( isReactActEnvironment : boolean | undefined ) {
14- globalThis . IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment ;
36+ getGlobalThis ( ) . IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment ;
1537}
1638
1739function getIsReactActEnvironment ( ) {
18- return globalThis . IS_REACT_ACT_ENVIRONMENT ;
40+ return getGlobalThis ( ) . IS_REACT_ACT_ENVIRONMENT ;
1941}
2042
2143function withGlobalActEnvironment ( actImplementation : ReactAct ) {
2244 return ( callback : Parameters < ReactAct > [ 0 ] ) => {
2345 const previousActEnvironment = getIsReactActEnvironment ( ) ;
2446 setIsReactActEnvironment ( true ) ;
25-
26- // this code is riddled with eslint disabling comments because this doesn't use real promises but eslint thinks we do
2747 try {
2848 // The return value of `act` is always a thenable.
2949 let callbackNeedsToBeAwaited = false ;
3050 const actResult = actImplementation ( ( ) => {
3151 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' ) {
3954 callbackNeedsToBeAwaited = true ;
4055 }
4156 return result ;
@@ -44,8 +59,8 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
4459 if ( callbackNeedsToBeAwaited ) {
4560 const thenable = actResult ;
4661 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
4964 thenable . then (
5065 // eslint-disable-next-line promise/always-return
5166 ( returnValue ) => {
@@ -72,9 +87,7 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
7287 } ;
7388}
7489
75- const act : ReactAct = checkReactVersionAtLeast ( 18 , 0 )
76- ? ( withGlobalActEnvironment ( reactTestRendererAct ) as ReactAct )
77- : reactTestRendererAct ;
90+ const act : ReactAct = withGlobalActEnvironment ( reactAct ) as ReactAct ;
7891
7992export default act ;
8093export { setIsReactActEnvironment as setReactActEnvironment , getIsReactActEnvironment } ;
0 commit comments