-
Notifications
You must be signed in to change notification settings - Fork 279
Do you have an example of how to test an Error Boundary? #779
-
I want to know the proper way to test the error boundary component.
Thank you very much in advance.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
I'm not aware of such example, but I think that building such a simple example should not be difficult. Error boundary catches exceptions thrown during render operations. So the test of custom error boundary component would look like this:
- Define trigger component that always throws during render. Or if you want to be fancy, it can render correctly initially and then throws during subsequent render after some internal state switch. Be sure not to throw during event as that is not covered by error boundary.
- Render trigger component inside error boundary, let the trigger component fail
- Expect that output is rendered (i.e. render does not throw but the error is consumed by error boundary), and the the rendered output is that of error boundary component and not of trigger component.
Beta Was this translation helpful? Give feedback.
All reactions
-
On top of that, what you need to keep in mind is that an error boundary is an implementation detail. What you want to test is that if some component throws at render time, your parent component behaves as expected. So if your error boundary renders an error message what you want to test is that this error message gets displayed.
RNTL will error if you have an unhandled error in your tree. So rendering a component that throws, handling the error and asserting the expecting output will work.
Beta Was this translation helpful? Give feedback.