@@ -9,18 +9,22 @@ describe('error hook tests', () => {
9
9
return true
10
10
}
11
11
12
- const somePromise = ( ) => Promise . resolve ( )
13
-
14
12
function useAsyncError ( throwError ) {
15
13
const [ value , setValue ] = useState ( )
16
14
useEffect ( ( ) => {
17
- somePromise ( ) . then ( ( ) => {
18
- setValue ( throwError )
19
- } )
15
+ const timeout = setTimeout ( ( ) => setValue ( throwError ) , 100 )
16
+ return ( ) => clearTimeout ( timeout )
20
17
} , [ throwError ] )
21
18
return useError ( value )
22
19
}
23
20
21
+ function useEffectError ( throwError ) {
22
+ useEffect ( ( ) => {
23
+ useError ( throwError )
24
+ } , [ ] )
25
+ return true
26
+ }
27
+
24
28
describe ( 'synchronous' , ( ) => {
25
29
test ( 'should raise error' , ( ) => {
26
30
const { result } = renderHook ( ( ) => useError ( true ) )
@@ -105,4 +109,47 @@ describe('error hook tests', () => {
105
109
expect ( result . error ) . toBe ( undefined )
106
110
} )
107
111
} )
112
+
113
+ /*
114
+ These tests capture error cases that are not currently being caught successfully.
115
+ Refer to https://github.com/testing-library/react-hooks-testing-library/issues/308
116
+ for more details.
117
+ */
118
+ describe . skip ( 'effect' , ( ) => {
119
+ test ( 'should raise effect error' , ( ) => {
120
+ const { result } = renderHook ( ( ) => useEffectError ( true ) )
121
+
122
+ expect ( ( ) => {
123
+ expect ( result . current ) . not . toBe ( undefined )
124
+ } ) . toThrow ( Error ( 'expected' ) )
125
+ } )
126
+
127
+ test ( 'should capture effect error' , ( ) => {
128
+ const { result } = renderHook ( ( ) => useEffectError ( true ) )
129
+ expect ( result . error ) . toEqual ( Error ( 'expected' ) )
130
+ } )
131
+
132
+ test ( 'should not capture effect error' , ( ) => {
133
+ const { result } = renderHook ( ( ) => useEffectError ( false ) )
134
+
135
+ expect ( result . current ) . not . toBe ( undefined )
136
+ expect ( result . error ) . toBe ( undefined )
137
+ } )
138
+
139
+ test ( 'should reset effect error' , ( ) => {
140
+ const { result, waitForNextUpdate, rerender } = renderHook (
141
+ ( throwError ) => useEffectError ( throwError ) ,
142
+ {
143
+ initialProps : true
144
+ }
145
+ )
146
+
147
+ expect ( result . error ) . not . toBe ( undefined )
148
+
149
+ rerender ( false )
150
+
151
+ expect ( result . current ) . not . toBe ( undefined )
152
+ expect ( result . error ) . toBe ( undefined )
153
+ } )
154
+ } )
108
155
} )
0 commit comments