-
Notifications
You must be signed in to change notification settings - Fork 232
Errors thrown in useEffect are not being caught #305
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
Comments
I took a look at this and it appears as though any errors being thrown in an effect (but not asynchronously) are not getting caught by the error handling in the test component. A quick test showed that using an actual error boundary does catch this error so we could look into that to support this (and we should support this). Funnily enough, we used to have an error boundary but I removed it because of #50 and #74. |
but is it a bug or an enhancement? |
I feel like it is a bug, but you're right, it's probably technically an enhancement. Does wrapping the Alternatively you should be able to use the I'm on my phone right now, but I can make an example for you later if that helps? |
Sure, i will be waiting |
Here is an example using an error boundary: it('should throw when over 100', () => {
let caughtError = null
class ErrorBoundary extends React.Component {
constructor(props) {
super(props)
this.state = { hasError: false }
}
componentDidCatch(error) {
this.setState({ hasError: true })
caughtError = error
}
render() {
return !this.state.hasError && this.props.children
}
}
const wrapper = ({ children }) => <ErrorBoundary>{children}</ErrorBoundary>
renderHook(() => useCounter(101), { wrapper })
expect(caughtError).toEqual(Error("It's over 100!"))
}) The following examples both rely on the error being raised out of the it('should throw when over 100', () => {
let caughtError = null
try {
renderHook(() => useCounter(101))
} catch (error) {
caughtError = error
}
expect(caughtError).toEqual(Error("It's over 100!"))
}) it('should throw when over 100', () => {
expect(() => renderHook(() => useCounter(101))).toThrow(Error("It's over 100!"))
}) I'll let you decide which approach works best for your situation. I've thought about it a bit more, and when a fix for this goes out, I will treat it as an enhancement and breaking change so that anyone relying on this behaviour for the workaround wont suddenly find they have breaking tests because the error it no longer being raised. |
@mpeyper - thank you! |
How to catch an
throw Error
which is thrown at first render ? for exampleand then i call the following code
So how to catch an error that is thrown automatically on first render?
The text was updated successfully, but these errors were encountered: