-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for React error handlers
- Loading branch information
Showing
4 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import * as React from 'react' | ||
import {render, renderHook} from '../' | ||
|
||
const isReact19 = React.version.startsWith('19.') | ||
|
||
const testGateReact19 = isReact19 ? test : test.skip | ||
|
||
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>' | ||
render(<div>client</div>, { | ||
container, | ||
hydrate: true, | ||
onRecoverableError, | ||
}) | ||
|
||
expect(onRecoverableError).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
message: expect.stringContaining('Hydration failed'), | ||
}), | ||
{ | ||
componentStack: expect.any(String), | ||
}, | ||
undefined, | ||
undefined, | ||
undefined, | ||
) | ||
}) | ||
|
||
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, | ||
}, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters