Skip to content

Commit 4afda1d

Browse files
committed
feat(console): export helper to manually suppress error output
Fixes #564
1 parent 7f6e4bb commit 4afda1d

File tree

8 files changed

+30
-24
lines changed

8 files changed

+30
-24
lines changed

src/core/console.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
import filterConsole from 'filter-console'
22

3+
function suppressErrorOutput() {
4+
if (process.env.RHTL_DISABLE_ERROR_FILTERING) {
5+
return () => {}
6+
}
7+
8+
return filterConsole(
9+
[
10+
/^The above error occurred in the <TestComponent> component:/, // error boundary output
11+
/^Error: Uncaught .+/ // jsdom output
12+
],
13+
{
14+
methods: ['error']
15+
}
16+
)
17+
}
18+
319
function enableErrorOutputSuppression() {
420
// Automatically registers console error suppression and restoration in supported testing frameworks
5-
if (
6-
typeof beforeEach === 'function' &&
7-
typeof afterEach === 'function' &&
8-
!process.env.RHTL_DISABLE_ERROR_FILTERING
9-
) {
10-
let restoreConsole: () => void
21+
if (typeof beforeEach === 'function' && typeof afterEach === 'function') {
22+
let restoreConsole!: () => void
1123

1224
beforeEach(() => {
13-
restoreConsole = filterConsole(
14-
[
15-
/^The above error occurred in the <TestComponent> component:/, // error boundary output
16-
/^Error: Uncaught .+/ // jsdom output
17-
],
18-
{
19-
methods: ['error']
20-
}
21-
)
25+
restoreConsole = suppressErrorOutput()
2226
})
2327

24-
afterEach(() => restoreConsole?.())
28+
afterEach(() => restoreConsole())
2529
}
2630
}
2731

28-
export { enableErrorOutputSuppression }
32+
export { enableErrorOutputSuppression, suppressErrorOutput }

src/core/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CreateRenderer, Renderer, RenderResult, RenderHookOptions } from '../ty
22

33
import { asyncUtils } from './asyncUtils'
44
import { cleanup, addCleanup, removeCleanup } from './cleanup'
5+
import { suppressErrorOutput } from './console'
56

67
function resultContainer<TValue>() {
78
const results: Array<{ value?: TValue; error?: Error }> = []
@@ -81,4 +82,4 @@ function createRenderHook<
8182
return renderHook
8283
}
8384

84-
export { createRenderHook, cleanup, addCleanup, removeCleanup }
85+
export { createRenderHook, cleanup, addCleanup, removeCleanup, suppressErrorOutput }

src/dom/__tests__/errorSuppression.disabled.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ describe('error output suppression (disabled) tests', () => {
55

66
beforeAll(() => {
77
process.env.RHTL_DISABLE_ERROR_FILTERING = 'true'
8+
require('..')
89
})
910

1011
test('should not patch console.error', () => {
11-
require('..')
1212
expect(console.error).toBe(originalConsoleError)
1313
})
1414
})

src/dom/pure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ const renderHook = createRenderHook(createDomRenderer)
3737

3838
export { renderHook, act }
3939

40-
export { cleanup, addCleanup, removeCleanup } from '../core'
40+
export { cleanup, addCleanup, removeCleanup, suppressErrorOutput } from '../core'
4141

4242
export * from '../types/react'

src/native/pure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ const renderHook = createRenderHook(createNativeRenderer)
3636

3737
export { renderHook, act }
3838

39-
export { cleanup, addCleanup, removeCleanup } from '../core'
39+
export { cleanup, addCleanup, removeCleanup, suppressErrorOutput } from '../core'
4040

4141
export * from '../types/react'

src/pure.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function getRenderer() {
3232
}
3333
}
3434

35-
const { renderHook, act, cleanup, addCleanup, removeCleanup } = getRenderer()
35+
const { renderHook, act, cleanup, addCleanup, removeCleanup, suppressErrorOutput } = getRenderer()
3636

37-
export { renderHook, act, cleanup, addCleanup, removeCleanup }
37+
export { renderHook, act, cleanup, addCleanup, removeCleanup, suppressErrorOutput }
3838

3939
export * from './types/react'

src/server/pure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ const renderHook = createRenderHook(createServerRenderer)
6161

6262
export { renderHook, act }
6363

64-
export { cleanup, addCleanup, removeCleanup } from '../core'
64+
export { cleanup, addCleanup, removeCleanup, suppressErrorOutput } from '../core'
6565

6666
export * from '../types/react'

src/types/react.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type ReactHooksRenderer = {
2626
cleanup: () => void
2727
addCleanup: (callback: CleanupCallback) => () => void
2828
removeCleanup: (callback: CleanupCallback) => void
29+
suppressErrorOutput: () => void
2930
}
3031

3132
export * from '.'

0 commit comments

Comments
 (0)