@@ -248,6 +248,56 @@ describe('ErrorBoundary', () => {
248248 expect ( cause . message ) . toEqual ( error . message ) ;
249249 } ) ;
250250
251+ // Regression test against:
252+ // https://github.com/getsentry/sentry-javascript/issues/6167
253+ it ( 'does not set cause if non Error objected is thrown' , ( ) => {
254+ const TestAppThrowingString : React . FC < ErrorBoundaryProps > = ( { children, ...props } ) => {
255+ const [ isError , setError ] = React . useState ( false ) ;
256+ function StringBam ( ) : JSX . Element {
257+ throw 'bam' ;
258+ }
259+ return (
260+ < ErrorBoundary
261+ { ...props }
262+ onReset = { ( ...args ) => {
263+ setError ( false ) ;
264+ if ( props . onReset ) {
265+ props . onReset ( ...args ) ;
266+ }
267+ } }
268+ >
269+ { isError ? < StringBam /> : children }
270+ < button
271+ data-testid = "errorBtn"
272+ onClick = { ( ) => {
273+ setError ( true ) ;
274+ } }
275+ />
276+ </ ErrorBoundary >
277+ ) ;
278+ } ;
279+
280+ render (
281+ < TestAppThrowingString fallback = { < p > You have hit an error</ p > } >
282+ < h1 > children</ h1 >
283+ </ TestAppThrowingString > ,
284+ ) ;
285+
286+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
287+
288+ const btn = screen . getByTestId ( 'errorBtn' ) ;
289+ fireEvent . click ( btn ) ;
290+
291+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
292+ expect ( mockCaptureException ) . toHaveBeenLastCalledWith ( 'bam' , {
293+ contexts : { react : { componentStack : expect . any ( String ) } } ,
294+ } ) ;
295+
296+ // Check if error.cause -> react component stack
297+ const error = mockCaptureException . mock . calls [ 0 ] [ 0 ] ;
298+ expect ( error . cause ) . not . toBeDefined ( ) ;
299+ } ) ;
300+
251301 it ( 'calls `beforeCapture()` when an error occurs' , ( ) => {
252302 const mockBeforeCapture = jest . fn ( ) ;
253303
0 commit comments