@@ -45,31 +45,40 @@ const navigationEvent: NavigationEvent = {
4545 url : new URL ( 'http://example.org/users/123' ) ,
4646} ;
4747
48+ const consoleErrorSpy = vi . spyOn ( console , 'error' ) . mockImplementation ( _ => { } ) ;
49+
4850describe ( 'handleError' , ( ) => {
4951 beforeEach ( ( ) => {
5052 mockCaptureException . mockClear ( ) ;
5153 mockAddExceptionMechanism . mockClear ( ) ;
54+ consoleErrorSpy . mockClear ( ) ;
5255 mockScope = new Scope ( ) ;
5356 } ) ;
5457
55- it ( 'works when a handleError func is not provided' , async ( ) => {
56- const wrappedHandleError = handleErrorWithSentry ( ) ;
57- const mockError = new Error ( 'test' ) ;
58- const returnVal = await wrappedHandleError ( { error : mockError , event : navigationEvent } ) ;
58+ describe ( 'calls captureException' , ( ) => {
59+ it ( 'invokes the default handler if no handleError func is provided' , async ( ) => {
60+ const wrappedHandleError = handleErrorWithSentry ( ) ;
61+ const mockError = new Error ( 'test' ) ;
62+ const returnVal = await wrappedHandleError ( { error : mockError , event : navigationEvent } ) ;
5963
60- expect ( returnVal ) . not . toBeDefined ( ) ;
61- expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
62- expect ( mockCaptureException ) . toHaveBeenCalledWith ( mockError , expect . any ( Function ) ) ;
63- } ) ;
64+ expect ( returnVal ) . not . toBeDefined ( ) ;
65+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
66+ expect ( mockCaptureException ) . toHaveBeenCalledWith ( mockError , expect . any ( Function ) ) ;
67+ // The default handler logs the error to the console
68+ expect ( consoleErrorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
69+ } ) ;
6470
65- it ( 'calls captureException ' , async ( ) => {
66- const wrappedHandleError = handleErrorWithSentry ( handleError ) ;
67- const mockError = new Error ( 'test' ) ;
68- const returnVal = ( await wrappedHandleError ( { error : mockError , event : navigationEvent } ) ) as any ;
71+ it ( 'invokes the user-provided error handler ' , async ( ) => {
72+ const wrappedHandleError = handleErrorWithSentry ( handleError ) ;
73+ const mockError = new Error ( 'test' ) ;
74+ const returnVal = ( await wrappedHandleError ( { error : mockError , event : navigationEvent } ) ) as any ;
6975
70- expect ( returnVal . message ) . toEqual ( 'Whoops!' ) ;
71- expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
72- expect ( mockCaptureException ) . toHaveBeenCalledWith ( mockError , expect . any ( Function ) ) ;
76+ expect ( returnVal . message ) . toEqual ( 'Whoops!' ) ;
77+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
78+ expect ( mockCaptureException ) . toHaveBeenCalledWith ( mockError , expect . any ( Function ) ) ;
79+ // Check that the default handler wasn't invoked
80+ expect ( consoleErrorSpy ) . toHaveBeenCalledTimes ( 0 ) ;
81+ } ) ;
7382 } ) ;
7483
7584 it ( 'adds an exception mechanism' , async ( ) => {
0 commit comments