import { stepResult } from "./constructors" ; import { isPromise } from "./isPromise" ; import { AttachmentOptions , ContentType , FixtureResult , Stage , Status , StatusDetails , StepResult , TestResult , } from "./model" ; export class ExecutableItemWrapper { constructor ( private readonly info ) { } protected get wrappedItem ( ) : FixtureResult | TestResult { return this . info ; } public set name ( name ) { this . info . name = name ; } public set description ( description ) { this . info . description = description ; } public set descriptionHtml ( descriptionHtml ) { this . info . descriptionHtml = descriptionHtml ; } public set status ( status ) { this . info . status = status ; } public get status ( ) : Status | undefined { return this . info . status ; } public set statusDetails ( details ) { this . info . statusDetails = details ; } public set detailsMessage ( message ) { this . info . statusDetails . message = message ; } public set detailsTrace ( trace ) { this . info . statusDetails . trace = trace ; } public set stage ( stage ) { this . info . stage = stage ; } public addParameter ( name , value ) { this . info . parameters . push ( { name , value } ) ; } public addAttachment ( name , options , fileName ) { if ( typeof options === "string" ) { options = { contentType : options } ; } this . info . attachments . push ( { name , type : options . contentType , source : fileName } ) ; } public startStep ( name , start ? ) { const result = stepResult ( ) ; this . info . steps . push ( result ) ; const allureStep = new AllureStep ( result , start ) ; allureStep . name = name ; return allureStep ; } public wrap < T > ( fun ) { return ( ... args ) : T => { this . stage = Stage . RUNNING ; let result ; try { result = fun ( args ) ; } catch ( error ) { this . stage = Stage . INTERRUPTED ; this . status = Status . BROKEN ; if ( error ) { this . detailsMessage = ( error as Error ) . message || "" ; this . detailsTrace = ( error as Error ) . stack || "" ; } throw error ; } if ( isPromise ( result ) ) { const promise = result as any as Promise < any > ; return promise . then ( ( res ) => { this . status = Status . PASSED ; this . stage = Stage . FINISHED ; return res ; } ) . catch ( ( error ) => { this . stage = Stage . INTERRUPTED ; this . status = Status . BROKEN ; if ( error ) { this . detailsMessage = ( error as Error ) . message || "" ; this . detailsTrace = ( error as Error ) . stack || "" ; } throw error ; } ) as any as T ; } else { this . status = Status . PASSED ; this . stage = Stage . FINISHED ; return result ; } } ; } } export class AllureStep extends ExecutableItemWrapper { constructor ( private readonly stepResult , start = Date . now ( ) ) { super ( stepResult ) ; this . stepResult . start = start ; } endStep ( stop = Date . now ( ) ) { this . stepResult . stop = stop ; } }