componentDidCatch and act not working as expected
See original GitHub issue👋 I think I found a bug 🙂
https://github.com/calebeby/preact-act-bug
I have a feeling it is more of a bug with componentDidCatch
than it is with act
, but maybe not.
const ThrowingComponent = () => {
throw new Error('its a broken component')
}
class ErrorBoundary extends Component {
componentDidCatch(error) {}
render() {
return this.props.children
}
}
test('act', () => {
jest.spyOn(console, 'error').mockImplementation(() => {})
act(() => {
const target = document.createElement('div')
render(
<ErrorBoundary>
<ThrowingComponent />
</ErrorBoundary>,
target,
)
})
})
In React, this passes, in Preact, it doesn’t. Somehow the error is escaping past the componentDidCatch
and up into the test, causing the test to fail. Any idea why?
It isn’t because of act
. The 2nd test in each file in the repro repo doesn’t use act, and you can still see the issue (the uncaught promise error in the console). I think act
is just making the issue more visible by synchronously causing the error to show itself.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Error Boundaries - React
To solve this problem for React users, React 16 introduces a new concept of an “error boundary”. ... Use componentDidCatch() to log error...
Read more >Render Sub-component Error in React.js using Error Boundaries
Try-catch should be used to handle any potential issues that can occur due to invalid input/data, connection issues or bad server response, etc....
Read more >Handling JavaScript errors in React with error boundaries
Use error boundaries in React components to handle JavaScript errors, act on them, and display a fallback user interface.
Read more >Why is this component being rendered after throwing an error?
I'm currently following along with the React JS documentation and I've encountered an issue with the error boundaries not working as expected. I ......
Read more >Test a componentDidCatch ErrorBoundary component in React
How to test a React componentDidCatch lifecycle method.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Still works though
@JoviDeCroock your test passes because there isn’t anything that throws 😕
ThrowingComponent
rendersundefined