Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Do you have an example of how to test an Error Boundary? #779

Unanswered
LaurenceM10 asked this question in Q&A
Discussion options

I want to know the proper way to test the error boundary component.

Thank you very much in advance.

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

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:

  1. 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.
  2. Render trigger component inside error boundary, let the trigger component fail
  3. 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.
You must be logged in to vote
1 reply
Comment options

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /