|
| 1 | +/* eslint-disable jest/no-if */ |
| 2 | +/* eslint-disable jest/no-conditional-in-test */ |
| 3 | +/* eslint-disable jest/no-conditional-expect */ |
| 4 | +import * as React from 'react' |
| 5 | +import {render, renderHook} from '../' |
| 6 | + |
| 7 | +const isReact19 = React.version.startsWith('19.') |
| 8 | + |
| 9 | +const testGateReact19 = isReact19 ? test : test.skip |
| 10 | + |
| 11 | +test('render errors', () => { |
| 12 | + function Thrower() { |
| 13 | + throw new Error('Boom!') |
| 14 | + } |
| 15 | + |
| 16 | + if (isReact19) { |
| 17 | + expect(() => { |
| 18 | + render(<Thrower />) |
| 19 | + }).toThrow('Boom!') |
| 20 | + } else { |
| 21 | + expect(() => { |
| 22 | + expect(() => { |
| 23 | + render(<Thrower />) |
| 24 | + }).toThrow('Boom!') |
| 25 | + }).toErrorDev([ |
| 26 | + 'Error: Uncaught [Error: Boom!]', |
| 27 | + // React retries on error |
| 28 | + 'Error: Uncaught [Error: Boom!]', |
| 29 | + ]) |
| 30 | + } |
| 31 | +}) |
| 32 | + |
| 33 | +test('onUncaughtError is not supported in render', () => { |
| 34 | + function Thrower() { |
| 35 | + throw new Error('Boom!') |
| 36 | + } |
| 37 | + const onUncaughtError = jest.fn(() => {}) |
| 38 | + |
| 39 | + expect(() => { |
| 40 | + render(<Thrower />, { |
| 41 | + onUncaughtError(error, errorInfo) { |
| 42 | + console.log({error, errorInfo}) |
| 43 | + }, |
| 44 | + }) |
| 45 | + }).toThrow( |
| 46 | + 'onUncaughtError is not supported. The `render` call will already throw on uncaught errors.', |
| 47 | + ) |
| 48 | + |
| 49 | + expect(onUncaughtError).toHaveBeenCalledTimes(0) |
| 50 | +}) |
| 51 | + |
| 52 | +testGateReact19('onCaughtError is supported in render', () => { |
| 53 | + const thrownError = new Error('Boom!') |
| 54 | + const handleComponentDidCatch = jest.fn() |
| 55 | + const onCaughtError = jest.fn() |
| 56 | + class ErrorBoundary extends React.Component { |
| 57 | + state = {error: null} |
| 58 | + static getDerivedStateFromError(error) { |
| 59 | + return {error} |
| 60 | + } |
| 61 | + componentDidCatch(error, errorInfo) { |
| 62 | + handleComponentDidCatch(error, errorInfo) |
| 63 | + } |
| 64 | + render() { |
| 65 | + if (this.state.error) { |
| 66 | + return null |
| 67 | + } |
| 68 | + return this.props.children |
| 69 | + } |
| 70 | + } |
| 71 | + function Thrower() { |
| 72 | + throw thrownError |
| 73 | + } |
| 74 | + |
| 75 | + render( |
| 76 | + <ErrorBoundary> |
| 77 | + <Thrower /> |
| 78 | + </ErrorBoundary>, |
| 79 | + { |
| 80 | + onCaughtError, |
| 81 | + }, |
| 82 | + ) |
| 83 | + |
| 84 | + expect(onCaughtError).toHaveBeenCalledWith(thrownError, { |
| 85 | + componentStack: expect.any(String), |
| 86 | + errorBoundary: expect.any(Object), |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +test('onRecoverableError is supported in render', () => { |
| 91 | + const onRecoverableError = jest.fn() |
| 92 | + |
| 93 | + const container = document.createElement('div') |
| 94 | + container.innerHTML = '<div>server</div>' |
| 95 | + // We just hope we forwarded the callback correctly (which is guaranteed since we just pass it along) |
| 96 | + // Frankly, I'm too lazy to assert on React 18 hydration errors since they're a mess. |
| 97 | + // eslint-disable-next-line jest/no-conditional-in-test |
| 98 | + if (isReact19) { |
| 99 | + render(<div>client</div>, { |
| 100 | + container, |
| 101 | + hydrate: true, |
| 102 | + onRecoverableError, |
| 103 | + }) |
| 104 | + expect(onRecoverableError).toHaveBeenCalledTimes(1) |
| 105 | + } else { |
| 106 | + expect(() => { |
| 107 | + render(<div>client</div>, { |
| 108 | + container, |
| 109 | + hydrate: true, |
| 110 | + onRecoverableError, |
| 111 | + }) |
| 112 | + }).toErrorDev(['', ''], {withoutStack: 1}) |
| 113 | + expect(onRecoverableError).toHaveBeenCalledTimes(2) |
| 114 | + } |
| 115 | +}) |
| 116 | + |
| 117 | +test('onUncaughtError is not supported in renderHook', () => { |
| 118 | + function useThrower() { |
| 119 | + throw new Error('Boom!') |
| 120 | + } |
| 121 | + const onUncaughtError = jest.fn(() => {}) |
| 122 | + |
| 123 | + expect(() => { |
| 124 | + renderHook(useThrower, { |
| 125 | + onUncaughtError(error, errorInfo) { |
| 126 | + console.log({error, errorInfo}) |
| 127 | + }, |
| 128 | + }) |
| 129 | + }).toThrow( |
| 130 | + 'onUncaughtError is not supported. The `render` call will already throw on uncaught errors.', |
| 131 | + ) |
| 132 | + |
| 133 | + expect(onUncaughtError).toHaveBeenCalledTimes(0) |
| 134 | +}) |
| 135 | + |
| 136 | +testGateReact19('onCaughtError is supported in renderHook', () => { |
| 137 | + const thrownError = new Error('Boom!') |
| 138 | + const handleComponentDidCatch = jest.fn() |
| 139 | + const onCaughtError = jest.fn() |
| 140 | + class ErrorBoundary extends React.Component { |
| 141 | + state = {error: null} |
| 142 | + static getDerivedStateFromError(error) { |
| 143 | + return {error} |
| 144 | + } |
| 145 | + componentDidCatch(error, errorInfo) { |
| 146 | + handleComponentDidCatch(error, errorInfo) |
| 147 | + } |
| 148 | + render() { |
| 149 | + if (this.state.error) { |
| 150 | + return null |
| 151 | + } |
| 152 | + return this.props.children |
| 153 | + } |
| 154 | + } |
| 155 | + function useThrower() { |
| 156 | + throw thrownError |
| 157 | + } |
| 158 | + |
| 159 | + renderHook(useThrower, { |
| 160 | + onCaughtError, |
| 161 | + wrapper: ErrorBoundary, |
| 162 | + }) |
| 163 | + |
| 164 | + expect(onCaughtError).toHaveBeenCalledWith(thrownError, { |
| 165 | + componentStack: expect.any(String), |
| 166 | + errorBoundary: expect.any(Object), |
| 167 | + }) |
| 168 | +}) |
| 169 | + |
| 170 | +// Currently, there's no recoverable error without hydration. |
| 171 | +// The option is still supported though. |
| 172 | +test('onRecoverableError is supported in renderHook', () => { |
| 173 | + const onRecoverableError = jest.fn() |
| 174 | + |
| 175 | + renderHook( |
| 176 | + () => { |
| 177 | + // TODO: trigger recoverable error |
| 178 | + }, |
| 179 | + { |
| 180 | + onRecoverableError, |
| 181 | + }, |
| 182 | + ) |
| 183 | +}) |
0 commit comments