import { Response } from 'express' ; import { request , Dino , Controller , ApiController , HttpGet , ErrorController } from './index' ; import { initializeTests } from './init' ; const nextError = 'NextError' ; const testErr = 'TestError' ; @ Controller ( '/test' ) class TestController extends ApiController { @ HttpGet ( '/get' ) query ( ) { throw new Error ( testErr ) ; } @ HttpGet ( '/next' ) nextErr ( ) { throw new Error ( nextError ) ; } } class AppError extends ErrorController { internalServerError ( ) { if ( this . error . message === nextError ) { this . next ( this . error ) ; } else { this . response . status ( 500 ) . json ( { data : this . error . message , id : this . request . query . id } ) ; } } } describe ( 'error.controller.e2e.spec' , ( ) => { const baseRoute = '/api/test' ; function register ( dino ) { dino . registerController ( TestController ) ; dino . registerApplicationError ( AppError ) ; dino . bind ( ) ; } it ( '/get_throws_error_returns_500_and_data' , done => { const x = initializeTests ( ) ; const app = x . app ; const dino = x . dino ; register ( dino ) ; request ( app ) . get ( `${ baseRoute }/get?id=42` ) . expect ( 'Content-Type' , /json/ ) . expect ( 500 , { data : testErr , id : 42 } , done ) ; } ) ; it ( '/next_throws_error_to_registered_express_err_when_app.use_after_dino.bind' , done => { const x = initializeTests ( ) ; const app = x . app ; const dino = x . dino ; register ( dino ) ; app . use ( ( err , req , res , next ) => { res . status ( 500 ) . json ( { data : 'From_Express' , error : nextError } ) ; } ) ; request ( app ) . get ( `${ baseRoute }/next` ) . expect ( 'Content-Type' , /json/ ) . expect ( 500 , { data : 'From_Express' , error : nextError } , done ) ; } ) ; } ) ;