import { Request , NextFunction } from 'express' ; import { errorMiddleware } from '../../../src/middlewares/errors' ; describe ( 'Errors middleware' , ( ) => { let res ; let sendFn ; beforeEach ( ( ) => { sendFn = jest . fn ( ) ; res = { status : jest . fn ( ) . mockReturnValue ( { send : sendFn } ) , } ; } ) ; describe ( 'when single error is provided' , ( ) => { it ( 'should send 500 and with error messages' , ( ) => { const error = new Error ( 'Some error' ) ; errorMiddleware ( ) ( error , { } as Request , res as any , ( ( ) => { } ) as NextFunction ) ; expect ( res . status ) . toHaveBeenCalledWith ( 500 ) ; expect ( sendFn ) . toHaveBeenCalledWith ( { errors : [ error ] } ) ; } ) ; } ) ; describe ( 'when array of error is provided' , ( ) => { it ( 'should send 500 and with error messages' , ( ) => { const errors = [ new Error ( 'array of errors' ) , new Error ( 'array of errors 2' ) ] ; errorMiddleware ( ) ( errors , { } as Request , res as any , ( ( ) => { } ) as NextFunction ) ; expect ( res . status ) . toHaveBeenCalledWith ( 500 ) ; expect ( sendFn ) . toHaveBeenCalledWith ( { errors } ) ; } ) ; } ) ; } ) ;