1
1
import { useState , useEffect } from 'react'
2
- import { renderHook } from '..'
2
+ import { renderHook , act } from '..'
3
3
4
4
describe ( 'error hook tests' , ( ) => {
5
5
function useError ( throwError ?: boolean ) {
@@ -109,15 +109,15 @@ describe('error hook tests', () => {
109
109
} )
110
110
111
111
describe ( 'effect' , ( ) => {
112
- test ( 'should raise effect error' , ( ) => {
112
+ test ( 'this one - should raise effect error' , ( ) => {
113
113
const { result } = renderHook ( ( ) => useEffectError ( true ) )
114
114
115
115
expect ( ( ) => {
116
116
expect ( result . current ) . not . toBe ( undefined )
117
117
} ) . toThrow ( Error ( 'expected' ) )
118
118
} )
119
119
120
- test ( 'should capture effect error' , ( ) => {
120
+ test ( 'this one - should capture effect error' , ( ) => {
121
121
const { result } = renderHook ( ( ) => useEffectError ( true ) )
122
122
expect ( result . error ) . toEqual ( Error ( 'expected' ) )
123
123
} )
@@ -142,4 +142,51 @@ describe('error hook tests', () => {
142
142
expect ( result . error ) . toBe ( undefined )
143
143
} )
144
144
} )
145
+
146
+ describe ( 'error output suppression' , ( ) => {
147
+ test ( 'should allow console.error to be mocked' , async ( ) => {
148
+ const consoleError = console . error
149
+ console . error = jest . fn ( )
150
+
151
+ try {
152
+ const { rerender, unmount } = renderHook (
153
+ ( stage ) => {
154
+ useEffect ( ( ) => {
155
+ console . error ( `expected in effect` )
156
+ return ( ) => {
157
+ console . error ( `expected in unmount` )
158
+ }
159
+ } , [ ] )
160
+ console . error ( `expected in ${ stage } ` )
161
+ } ,
162
+ {
163
+ initialProps : 'render'
164
+ }
165
+ )
166
+
167
+ act ( ( ) => {
168
+ console . error ( 'expected in act' )
169
+ } )
170
+
171
+ await act ( async ( ) => {
172
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
173
+ console . error ( 'expected in async act' )
174
+ } )
175
+
176
+ rerender ( 'rerender' )
177
+
178
+ unmount ( )
179
+
180
+ expect ( console . error ) . toBeCalledWith ( 'expected in render' )
181
+ expect ( console . error ) . toBeCalledWith ( 'expected in effect' )
182
+ expect ( console . error ) . toBeCalledWith ( 'expected in act' )
183
+ expect ( console . error ) . toBeCalledWith ( 'expected in async act' )
184
+ expect ( console . error ) . toBeCalledWith ( 'expected in rerender' )
185
+ expect ( console . error ) . toBeCalledWith ( 'expected in unmount' )
186
+ expect ( console . error ) . toBeCalledTimes ( 6 )
187
+ } finally {
188
+ console . error = consoleError
189
+ }
190
+ } )
191
+ } )
145
192
} )
0 commit comments