import { ArgumentsHost } from '@nestjs/common' ; import { ApiErrorMessage , ApiErrorParams } from '@fc/app' ; import { ConfigService } from '@fc/config' ; import { LoggerService } from '@fc/logger' ; import { UnhandledExceptionFilter } from './unhandled.exception-filter' ; describe ( ' UnhandledExceptionFilter' , ( ) => { let exceptionFilter ; const loggerMock = { debug : jest . fn ( ) , error : jest . fn ( ) , setContext : jest . fn ( ) , trace : jest . fn ( ) , } as unknown as LoggerService ; const resMock = { } ; resMock . render = jest . fn ( ) . mockReturnValue ( resMock ) ; resMock . json = jest . fn ( ) . mockReturnValue ( resMock ) ; resMock . status = jest . fn ( ) . mockReturnValue ( resMock ) ; const argumentHostMock = { switchToHttp : ( ) => ( { getResponse : ( ) => resMock , } ) , } as ArgumentsHost ; const configServiceMock = { get : jest . fn ( ) , } ; const errorValueMock = { code : 'codeValueMock' , id : 'idValueMock' , message : 'messageValueMock' , } ; beforeEach ( ( ) => { jest . resetAllMocks ( ) ; jest . restoreAllMocks ( ) ; exceptionFilter = new UnhandledExceptionFilter ( configServiceMock as unknown as ConfigService , loggerMock ) ; configServiceMock . get . mockReturnValue ( { apiOutputContentType : 'html' , } ) ; } ) ; describe ( 'catch()' , ( ) => { it ( 'should log an error' , ( ) => { const exception = new Error ( 'message text' ) ; exceptionFilter . catch ( exception , argumentHostMock ) ; expect ( loggerMock . error ) . toHaveBeenCalledWith ( expect . objectContaining ( { type : 'Error' , code : 'Y000000' , message : 'message text' , } ) ) ; } ) ; it ( 'should render error template' , ( ) => { const exception = new Error ( 'message text' ) ; exceptionFilter . catch ( exception , argumentHostMock ) ; expect ( resMock . render ) . toHaveBeenCalledWith ( 'error' , expect . objectContaining ( { code : 'Y000000' , message : 'message text' , } ) ) ; } ) ; } ) ; describe ( 'errorOutput()' , ( ) => { it ( 'should return an error in JSON if the `apiOutputContentType` value is set to `json`' , ( ) => { configServiceMock . get . mockReturnValue ( { apiOutputContentType : 'json' , } ) ; const httpErrorCodeValueMock = 500 ; const exceptionParam = { res : resMock , error : errorValueMock , httpResponseCode : httpErrorCodeValueMock , } ; exceptionFilter [ 'errorOutput' ] ( exceptionParam ) ; expect ( resMock . json ) . toHaveBeenCalledTimes ( 1 ) ; expect ( resMock . json ) . toHaveBeenCalledWith ( { code : 'codeValueMock' , id : 'idValueMock' , message : 'messageValueMock' , } ) ; } ) ; it ( 'should return an error through `res.render` if the `apiOutputContentType` value is set to `html`' , ( ) => { const httpErrorCodeValueMock = 500 ; const exceptionParam = { res : resMock , error : errorValueMock , httpResponseCode : httpErrorCodeValueMock , } ; const errorValueReturnedMock = { code : 'codeValueMock' , id : 'idValueMock' , message : 'messageValueMock' , } ; exceptionFilter [ 'errorOutput' ] ( exceptionParam ) ; expect ( resMock . render ) . toHaveBeenCalledTimes ( 1 ) ; expect ( resMock . render ) . toHaveBeenCalledWith ( 'error' , expect . objectContaining ( errorValueReturnedMock ) ) ; } ) ; } ) ; } ) ;