1
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
2
+ // https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/types/index.d.ts
3
+ import * as React from 'react' ;
3
4
import { act as reactTestRendererAct } from 'react-test-renderer' ;
4
5
5
- type ReactAct = typeof reactTestRendererAct ;
6
+ const reactAct = typeof React . act === 'function' ? React . act : reactTestRendererAct ;
7
+ type ReactAct = 0 extends 1 & typeof React . act ? typeof reactTestRendererAct : typeof React . act ;
6
8
7
9
// See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT
8
10
declare global {
@@ -22,19 +24,13 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
22
24
const previousActEnvironment = getIsReactActEnvironment ( ) ;
23
25
setIsReactActEnvironment ( true ) ;
24
26
25
- // this code is riddled with eslint disabling comments because this doesn't use real promises but eslint thinks we do
26
27
try {
27
28
// The return value of `act` is always a thenable.
28
29
let callbackNeedsToBeAwaited = false ;
29
30
const actResult = actImplementation ( ( ) => {
30
31
const result = callback ( ) ;
31
- if (
32
- result !== null &&
33
- typeof result === 'object' &&
34
- // @ts -expect-error this should be a promise or thenable
35
- // eslint-disable-next-line promise/prefer-await-to-then
36
- typeof result . then === 'function'
37
- ) {
32
+ // @ts -expect-error TS is too strict here
33
+ if ( result !== null && typeof result === 'object' && typeof result . then === 'function' ) {
38
34
callbackNeedsToBeAwaited = true ;
39
35
}
40
36
return result ;
@@ -44,15 +40,17 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
44
40
const thenable = actResult ;
45
41
return {
46
42
then : ( resolve : ( value : never ) => never , reject : ( value : never ) => never ) => {
47
- // eslint-disable-next-line
43
+ // eslint-disable-next-line promise/catch-or-return, promise/prefer-await-to-then
48
44
thenable . then (
49
45
// eslint-disable-next-line promise/always-return
50
46
( returnValue ) => {
51
47
setIsReactActEnvironment ( previousActEnvironment ) ;
48
+ // @ts -expect-error
52
49
resolve ( returnValue ) ;
53
50
} ,
54
51
( error ) => {
55
52
setIsReactActEnvironment ( previousActEnvironment ) ;
53
+ // @ts -expect-error
56
54
reject ( error ) ;
57
55
} ,
58
56
) ;
@@ -71,7 +69,8 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
71
69
} ;
72
70
}
73
71
74
- const act = withGlobalActEnvironment ( reactTestRendererAct ) as ReactAct ;
72
+ // @ts -expect-error
73
+ const act = withGlobalActEnvironment ( reactAct ) as ReactAct ;
75
74
76
75
export default act ;
77
76
export { setIsReactActEnvironment as setReactActEnvironment , getIsReactActEnvironment } ;
0 commit comments