Skip to content

Commit 86c88f3

Browse files
committed
feat: add option to not suppress error output if desired
1 parent 6335c4e commit 86c88f3

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed

src/dom/__tests__/errorHook.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,42 @@ describe('error hook tests', () => {
142142
expect(result.error).toBe(undefined)
143143
})
144144
})
145+
146+
describe('error output suppression', () => {
147+
const originalConsoleError = console.error
148+
const mockConsoleError = jest.fn()
149+
150+
beforeEach(() => {
151+
console.error = mockConsoleError
152+
})
153+
154+
afterEach(() => {
155+
console.error = originalConsoleError
156+
})
157+
158+
test('should suppress error output', () => {
159+
const { result } = renderHook(() => useError(true), {
160+
suppressErrorOutput: true
161+
})
162+
163+
expect(result.error).toEqual(Error('expected'))
164+
expect(mockConsoleError).toBeCalledTimes(0)
165+
})
166+
167+
test('should not suppress error output', () => {
168+
const { result } = renderHook(() => useError(true), {
169+
suppressErrorOutput: false
170+
})
171+
172+
expect(result.error).toEqual(Error('expected'))
173+
expect(mockConsoleError).toBeCalledWith(
174+
expect.stringMatching(/^Error: Uncaught \[Error: expected\]/),
175+
expect.any(Error)
176+
)
177+
expect(mockConsoleError).toBeCalledWith(
178+
expect.stringMatching(/^The above error occurred in the <TestComponent> component:/)
179+
)
180+
expect(mockConsoleError).toBeCalledTimes(2)
181+
})
182+
})
145183
})

src/dom/pure.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { createTestHarness } from '../helpers/createTestHarness'
88

99
function createDomRenderer<TProps, TResult>(
1010
rendererProps: RendererProps<TProps, TResult>,
11-
{ wrapper }: RendererOptions<TProps>
11+
options: RendererOptions<TProps>
1212
) {
1313
const container = document.createElement('div')
14-
const testHarness = createTestHarness(rendererProps, wrapper)
14+
const testHarness = createTestHarness(rendererProps, options)
1515

1616
return {
1717
render(props?: TProps) {

src/helpers/createTestHarness.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import filterConsole from 'filter-console'
44

55
import { addCleanup } from '../core'
66

7-
import { RendererProps, WrapperComponent } from '../types/react'
7+
import { RendererProps, RendererOptions } from '../types/react'
88

9-
function suppressErrorOutput() {
9+
function filterErrorOutput() {
1010
// The error output from error boundaries is notoriously difficult to suppress. To save
11-
// out users from having to work it out, we crudely suppress the output matching the patterns
11+
// our users from having to work it out, we crudely suppress the output matching the patterns
1212
// below. For more information, see these issues:
1313
// - https://github.com/testing-library/react-hooks-testing-library/issues/50
1414
// - https://github.com/facebook/react/issues/11098#issuecomment-412682721
@@ -28,7 +28,7 @@ function suppressErrorOutput() {
2828

2929
function createTestHarness<TProps, TResult>(
3030
{ callback, setValue, setError }: RendererProps<TProps, TResult>,
31-
Wrapper?: WrapperComponent<TProps>,
31+
{ wrapper: Wrapper, suppressErrorOutput = true }: RendererOptions<TProps>,
3232
suspense: boolean = true
3333
) {
3434
const TestComponent = ({ hookProps }: { hookProps?: TProps }) => {
@@ -47,7 +47,9 @@ function createTestHarness<TProps, TResult>(
4747
return null
4848
}
4949

50-
suppressErrorOutput()
50+
if (suppressErrorOutput) {
51+
filterErrorOutput()
52+
}
5153

5254
const testHarness = (props?: TProps) => {
5355
resetErrorBoundary()

src/native/pure.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import { createTestHarness } from '../helpers/createTestHarness'
77

88
function createNativeRenderer<TProps, TResult>(
99
rendererProps: RendererProps<TProps, TResult>,
10-
{ wrapper }: RendererOptions<TProps>
10+
options: RendererOptions<TProps>
1111
) {
1212
let container: ReactTestRenderer
13-
const testHarness = createTestHarness(rendererProps, wrapper)
13+
const testHarness = createTestHarness(rendererProps, options)
1414

1515
return {
1616
render(props?: TProps) {

src/server/pure.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import { createTestHarness } from '../helpers/createTestHarness'
99

1010
function createServerRenderer<TProps, TResult>(
1111
rendererProps: RendererProps<TProps, TResult>,
12-
{ wrapper }: RendererOptions<TProps>
12+
options: RendererOptions<TProps>
1313
) {
1414
let renderProps: TProps | undefined
1515
let hydrated = false
1616
const container = document.createElement('div')
17-
const testHarness = createTestHarness(rendererProps, wrapper, false)
17+
const testHarness = createTestHarness(rendererProps, options, false)
1818

1919
return {
2020
render(props?: TProps) {

src/types/react.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type WrapperComponent<TProps> = ComponentType<TProps>
1111

1212
export type RendererOptions<TProps> = {
1313
wrapper?: WrapperComponent<TProps>
14+
suppressErrorOutput?: boolean
1415
}
1516

1617
export type RenderHookOptions<TProps> = BaseRenderHookOptions<TProps> & {

0 commit comments

Comments
 (0)