import Logger from '../../core/logging/logger'; import { isError } from '../../error/helpers'; import { reportError } from '../../error/reporting'; import { addCsErrorHandler, removeCsErrorHandler, resetModuleState, } from '../../error/utils'; jest.mock('../../core/logging/logger', () => ({ info: jest.fn(), })); jest.mock('../../error/helpers', () => ({ isError: jest.fn(), })); jest.mock('../../error/reporting', () => ({ reportError: jest.fn(), })); describe('csErrorHandler', () => { let originalHandlerMock: jest.Mock; beforeEach(() => { originalHandlerMock = jest.fn(); // eslint-disable-next-line @typescript-eslint/no-explicit-any (ErrorUtils as any).getGlobalHandler = jest.fn(() => originalHandlerMock); // eslint-disable-next-line @typescript-eslint/no-explicit-any (ErrorUtils as any).setGlobalHandler = jest.fn(); }); afterEach(() => { resetModuleState(); jest.clearAllMocks(); }); describe('addCsErrorHandler', () => { it('should set the custom error handler and log an info message', () => { addCsErrorHandler(); expect(ErrorUtils.getGlobalHandler).toHaveBeenCalled(); expect(ErrorUtils.setGlobalHandler).toHaveBeenCalledWith( expect.any(Function) ); expect(Logger.info).toHaveBeenCalledWith( 'CSErrorHandler is set as JavaScript global error handler.' ); }); it('should report error and call the original handler for non-fatal errors', async () => { (isError as unknown as jest.Mock).mockReturnValue(true); addCsErrorHandler(); const wrappedHandler = (ErrorUtils.setGlobalHandler as jest.Mock).mock .calls[0][0]; const mockError = new Error('Fatal error'); await wrappedHandler(mockError, true); expect(isError).toHaveBeenCalledWith(mockError); expect(reportError).toHaveBeenCalledWith(mockError); expect(originalHandlerMock).toHaveBeenCalledWith(mockError, true); }); it('should call the original handler for non-fatal errors', async () => { (isError as unknown as jest.Mock).mockReturnValue(true); addCsErrorHandler(); const wrappedHandler = (ErrorUtils.setGlobalHandler as jest.Mock).mock .calls[0][0]; const mockError = new Error('Non-fatal error'); await wrappedHandler(mockError, false); expect(isError).toHaveBeenCalledWith(mockError); expect(originalHandlerMock).toHaveBeenCalledWith(mockError, false); expect(reportError).not.toHaveBeenCalled(); }); it('should call the original handler for non-Error instances', async () => { (isError as unknown as jest.Mock).mockReturnValue(false); addCsErrorHandler(); const wrappedHandler = (ErrorUtils.setGlobalHandler as jest.Mock).mock .calls[0][0]; const mockError = { message: 'Not an error' }; await wrappedHandler(mockError, true); expect(isError).toHaveBeenCalledWith(mockError); expect(originalHandlerMock).toHaveBeenCalledWith(mockError, true); expect(reportError).not.toHaveBeenCalled(); }); }); describe('removeCsErrorHandler', () => { it('should restore the original global error handler', () => { addCsErrorHandler(); removeCsErrorHandler(); expect(ErrorUtils.setGlobalHandler).toHaveBeenCalledWith( originalHandlerMock ); expect(Logger.info).toHaveBeenCalledWith( 'Original JavaScript global error handler has been restored.' ); }); it('should not restore the original global error handler if not set', () => { removeCsErrorHandler(); expect(ErrorUtils.setGlobalHandler).not.toHaveBeenCalled(); expect(Logger.info).not.toHaveBeenCalled(); }); }); });