File tree 2 files changed +27
-1
lines changed
2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 1
1
import * as React from 'react'
2
- import { render , fireEvent , screen } from '../'
2
+ import { act , render , fireEvent , screen } from '../'
3
3
4
4
test ( 'render calls useEffect immediately' , ( ) => {
5
5
const effectCb = jest . fn ( )
@@ -43,3 +43,27 @@ test('calls to hydrate will run useEffects', () => {
43
43
render ( < MyUselessComponent /> , { hydrate : true } )
44
44
expect ( effectCb ) . toHaveBeenCalledTimes ( 1 )
45
45
} )
46
+
47
+ test ( 'cleans up IS_REACT_ACT_ENVIRONMENT if its callback throws' , ( ) => {
48
+ global . IS_REACT_ACT_ENVIRONMENT = false
49
+
50
+ expect ( ( ) =>
51
+ act ( ( ) => {
52
+ throw new Error ( 'threw' )
53
+ } ) ,
54
+ ) . toThrow ( 'threw' )
55
+
56
+ expect ( global . IS_REACT_ACT_ENVIRONMENT ) . toEqual ( false )
57
+ } )
58
+
59
+ test ( 'cleans up IS_REACT_ACT_ENVIRONMENT if its async callback throws' , async ( ) => {
60
+ global . IS_REACT_ACT_ENVIRONMENT = false
61
+
62
+ await expect ( ( ) =>
63
+ act ( async ( ) => {
64
+ throw new Error ( 'thenable threw' )
65
+ } ) ,
66
+ ) . rejects . toThrow ( 'thenable threw' )
67
+
68
+ expect ( global . IS_REACT_ACT_ENVIRONMENT ) . toEqual ( false )
69
+ } )
Original file line number Diff line number Diff line change @@ -79,6 +79,8 @@ function withGlobalActEnvironment(actImplementation) {
79
79
return actResult
80
80
}
81
81
} catch ( error ) {
82
+ // Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT
83
+ // or if we have to await the callback first.
82
84
setReactActEnvironment ( previousActEnvironment )
83
85
throw error
84
86
}
You can’t perform that action at this time.
0 commit comments