|
1 | 1 | import React, { Suspense } from 'react'
|
| 2 | +import { ErrorBoundary, FallbackProps } from 'react-error-boundary' |
| 3 | +import filterConsole from 'filter-console' |
2 | 4 |
|
3 |
| -import { RendererProps, WrapperComponent } from '../types/react' |
| 5 | +import { addCleanup } from '../core' |
4 | 6 |
|
5 |
| -import { isPromise } from './promises' |
| 7 | +import { RendererProps, WrapperComponent } from '../types/react' |
6 | 8 |
|
7 |
| -function TestComponent<TProps, TResult>({ |
8 |
| - hookProps, |
9 |
| - callback, |
10 |
| - setError, |
11 |
| - setValue |
12 |
| -}: RendererProps<TProps, TResult> & { hookProps?: TProps }) { |
13 |
| - try { |
14 |
| - // coerce undefined into TProps, so it maintains the previous behaviour |
15 |
| - setValue(callback(hookProps as TProps)) |
16 |
| - } catch (err: unknown) { |
17 |
| - if (isPromise(err)) { |
18 |
| - throw err |
19 |
| - } else { |
20 |
| - setError(err as Error) |
| 9 | +function suppressErrorOutput() { |
| 10 | + // 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 |
| 12 | + // below. For more information, see these issues: |
| 13 | + // - https://github.com/testing-library/react-hooks-testing-library/issues/50 |
| 14 | + // - https://github.com/facebook/react/issues/11098#issuecomment-412682721 |
| 15 | + // - https://github.com/facebook/react/issues/15520 |
| 16 | + // - https://github.com/facebook/react/issues/18841 |
| 17 | + const removeConsoleFilter = filterConsole( |
| 18 | + [ |
| 19 | + /^The above error occurred in the <TestComponent> component:/, // error boundary output |
| 20 | + /^Error: Uncaught .+/ // jsdom output |
| 21 | + ], |
| 22 | + { |
| 23 | + methods: ['error'] |
21 | 24 | }
|
22 |
| - } |
23 |
| - return null |
| 25 | + ) |
| 26 | + addCleanup(removeConsoleFilter) |
24 | 27 | }
|
25 | 28 |
|
26 | 29 | function createTestHarness<TProps, TResult>(
|
27 |
| - rendererProps: RendererProps<TProps, TResult>, |
| 30 | + { callback, setValue, setError }: RendererProps<TProps, TResult>, |
28 | 31 | Wrapper?: WrapperComponent<TProps>,
|
29 | 32 | suspense: boolean = true
|
30 | 33 | ) {
|
| 34 | + const TestComponent = ({ hookProps }: { hookProps?: TProps }) => { |
| 35 | + // coerce undefined into TProps, so it maintains the previous behaviour |
| 36 | + setValue(callback(hookProps as TProps)) |
| 37 | + return null |
| 38 | + } |
| 39 | + |
| 40 | + let resetErrorBoundary = () => {} |
| 41 | + const ErrorFallback = ({ error, resetErrorBoundary: reset }: FallbackProps) => { |
| 42 | + resetErrorBoundary = () => { |
| 43 | + resetErrorBoundary = () => {} |
| 44 | + reset() |
| 45 | + } |
| 46 | + setError(error) |
| 47 | + return null |
| 48 | + } |
| 49 | + |
| 50 | + suppressErrorOutput() |
| 51 | + |
31 | 52 | const testHarness = (props?: TProps) => {
|
32 |
| - let component = <TestComponent hookProps={props} {...rendererProps} /> |
| 53 | + resetErrorBoundary() |
| 54 | + |
| 55 | + let component = <TestComponent hookProps={props} /> |
33 | 56 | if (Wrapper) {
|
34 | 57 | component = <Wrapper {...(props as TProps)}>{component}</Wrapper>
|
35 | 58 | }
|
36 | 59 | if (suspense) {
|
37 | 60 | component = <Suspense fallback={null}>{component}</Suspense>
|
38 | 61 | }
|
39 |
| - return component |
| 62 | + return <ErrorBoundary FallbackComponent={ErrorFallback}>{component}</ErrorBoundary> |
40 | 63 | }
|
41 | 64 |
|
42 | 65 | return testHarness
|
|
0 commit comments