Skip to content

Commit 096282c

Browse files
committed
docs: added docs for suppressErrorOutput
1 parent 4afda1d commit 096282c

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

docs/api-reference.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ route: '/reference/api'
1212
- [`cleanup`](/reference/api#cleanup)
1313
- [`addCleanup`](/reference/api#addcleanup)
1414
- [`removeCleanup`](/reference/api#removecleanup)
15+
- [`suppressErrorOutput`](/reference/api#manually-suppress-output)
1516

1617
---
1718

@@ -286,6 +287,10 @@ to filter out the unnecessary logging and restore the original version during cl
286287
side-effect can affect tests that also patch `console.error` (e.g. to assert a specific error
287288
message get logged) by replacing their custom implementation as well.
288289

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+
289294
### Disabling `console.error` filtering
290295

291296
Importing `@testing-library/react-hooks/disable-error-filtering.js` in test setup files disable the
@@ -303,8 +308,8 @@ module.exports = {
303308
}
304309
```
305310

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.
308313

309314
```diff
310315
- 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
316321
317322
> Please note that this may result in a significant amount of additional logging in your test
318323
> 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

Comments
 (0)