Skip to content

feat: add option to not suppress error output if desired #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/dom/__tests__/errorHook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,42 @@ describe('error hook tests', () => {
expect(result.error).toBe(undefined)
})
})

describe('error output suppression', () => {
const originalConsoleError = console.error
const mockConsoleError = jest.fn()

beforeEach(() => {
console.error = mockConsoleError
})

afterEach(() => {
console.error = originalConsoleError
})

test('should suppress error output', () => {
const { result } = renderHook(() => useError(true), {
suppressErrorOutput: true
})

expect(result.error).toEqual(Error('expected'))
expect(mockConsoleError).toBeCalledTimes(0)
})

test('should not suppress error output', () => {
const { result } = renderHook(() => useError(true), {
suppressErrorOutput: false
})

expect(result.error).toEqual(Error('expected'))
expect(mockConsoleError).toBeCalledWith(
expect.stringMatching(/^Error: Uncaught \[Error: expected\]/),
expect.any(Error)
)
expect(mockConsoleError).toBeCalledWith(
expect.stringMatching(/^The above error occurred in the <TestComponent> component:/)
)
expect(mockConsoleError).toBeCalledTimes(2)
})
})
})
4 changes: 2 additions & 2 deletions src/dom/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { createTestHarness } from '../helpers/createTestHarness'

function createDomRenderer<TProps, TResult>(
rendererProps: RendererProps<TProps, TResult>,
{ wrapper }: RendererOptions<TProps>
options: RendererOptions<TProps>
) {
const container = document.createElement('div')
const testHarness = createTestHarness(rendererProps, wrapper)
const testHarness = createTestHarness(rendererProps, options)

return {
render(props?: TProps) {
Expand Down
12 changes: 7 additions & 5 deletions src/helpers/createTestHarness.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import filterConsole from 'filter-console'

import { addCleanup } from '../core'

import { RendererProps, WrapperComponent } from '../types/react'
import { RendererProps, RendererOptions } from '../types/react'

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

function createTestHarness<TProps, TResult>(
{ callback, setValue, setError }: RendererProps<TProps, TResult>,
Wrapper?: WrapperComponent<TProps>,
{ wrapper: Wrapper, suppressErrorOutput = true }: RendererOptions<TProps>,
suspense: boolean = true
) {
const TestComponent = ({ hookProps }: { hookProps?: TProps }) => {
Expand All @@ -47,7 +47,9 @@ function createTestHarness<TProps, TResult>(
return null
}

suppressErrorOutput()
if (suppressErrorOutput) {
filterErrorOutput()
}

const testHarness = (props?: TProps) => {
resetErrorBoundary()
Expand Down
4 changes: 2 additions & 2 deletions src/native/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { createTestHarness } from '../helpers/createTestHarness'

function createNativeRenderer<TProps, TResult>(
rendererProps: RendererProps<TProps, TResult>,
{ wrapper }: RendererOptions<TProps>
options: RendererOptions<TProps>
) {
let container: ReactTestRenderer
const testHarness = createTestHarness(rendererProps, wrapper)
const testHarness = createTestHarness(rendererProps, options)

return {
render(props?: TProps) {
Expand Down
4 changes: 2 additions & 2 deletions src/server/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { createTestHarness } from '../helpers/createTestHarness'

function createServerRenderer<TProps, TResult>(
rendererProps: RendererProps<TProps, TResult>,
{ wrapper }: RendererOptions<TProps>
options: RendererOptions<TProps>
) {
let renderProps: TProps | undefined
let hydrated = false
const container = document.createElement('div')
const testHarness = createTestHarness(rendererProps, wrapper, false)
const testHarness = createTestHarness(rendererProps, options, false)

return {
render(props?: TProps) {
Expand Down
1 change: 1 addition & 0 deletions src/types/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type WrapperComponent<TProps> = ComponentType<TProps>

export type RendererOptions<TProps> = {
wrapper?: WrapperComponent<TProps>
suppressErrorOutput?: boolean
}

export type RenderHookOptions<TProps> = BaseRenderHookOptions<TProps> & {
Expand Down