@@ -12,6 +12,7 @@ route: '/reference/api'
12
12
- [ ` cleanup ` ] ( /reference/api#cleanup )
13
13
- [ ` addCleanup ` ] ( /reference/api#addcleanup )
14
14
- [ ` removeCleanup ` ] ( /reference/api#removecleanup )
15
+ - [ ` suppressErrorOutput ` ] ( /reference/api#manually-suppress-output )
15
16
16
17
---
17
18
@@ -286,6 +287,10 @@ to filter out the unnecessary logging and restore the original version during cl
286
287
side-effect can affect tests that also patch `console.error` (e.g. to assert a specific error
287
288
message get logged ) by replacing their custom implementation as well.
288
289
290
+ > Please note that this is done automatically if the testing framework you're using supports the
291
+ > `beforeEach` and `afterEach` global (like Jest , mocha and Jasmine ). If not, you will need to do
292
+ > [manual suppression](/reference /api #manually -suppress -output ) around the test run.
293
+
289
294
### Disabling `console.error` filtering
290
295
291
296
Importing `@testing-library/react-hooks/disable-error-filtering.js` in test setup files disable the
@@ -303,8 +308,8 @@ module.exports = {
303
308
}
304
309
` ` `
305
310
306
- Alternatively , you can change your test to import from ` @testing-library/react-hooks/pure ` (or any
307
- of the [other non -pure imports ](/installation #pure -imports )) instead of the regular imports.
311
+ Alternatively , you can change your test to import from ` @testing-library/react-hooks ` (or any of the
312
+ [other non -pure imports ](/installation #pure -imports )) instead of the regular imports.
308
313
309
314
```diff
310
315
- import { renderHook , cleanup , act } from ' @testing-library/react-hooks'
@@ -316,3 +321,25 @@ variable to `true` before importing `@testing-library/react-hooks` will also dis
316
321
317
322
> Please note that this may result in a significant amount of additional logging in your test
318
323
> output.
324
+
325
+ ### Manually suppress output
326
+
327
+ If you are using [a pure import](/installation#pure-imports), you are running your tests in an
328
+ environment that does not support ` beforeEach ` and ` afterEach ` , or if the automatic suppression is
329
+ not available to you for some other reason, then you can use the ` suppressErrorOutput ` export to
330
+ manually start and top suppress the output:
331
+
332
+ ` ` ` ts
333
+ import { renderHook , suppressErrorOutput } from ' @testing-library/react-hooks/pure'
334
+
335
+ test (' should handle thrown error' , () => {
336
+ const restoreConsole = suppressErrorOutput ()
337
+
338
+ try {
339
+ const { result } = renderHook (() => useCounter ())
340
+ expect (result .error ).toBeDefined ()
341
+ } finally {
342
+ restoreConsole ()
343
+ }
344
+ })
345
+ ` ` `
0 commit comments