Search code examples
reactjssentry

How to trigger an uncaught exception in React, for Sentry testing?


I need to trigger an error in a React app to make sure that my Sentry.io has been configured correctly.

Here is what the Sentry docs is asking me to do:

You can trigger your first event from your development 
environment by raising an exception somewhere within 
your application. An example of this would be rendering 
a button whose onClick handler attempts to invoke a method 
that does not exist:

return <button onClick={methodDoesNotExist}>Break the world</button>;

However, even if I disable eslint for the file, React itself will not let me define an element whose onClick is an undefined method.

How else can I generate such an error?


Solution

  • Simply add:

    <button onClick={()=>{
    throw 'oh no an error'`
    }}> Err </button>
    

    Thanks to @omnikron