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