import { Allure , AllureRuntime , AllureStep , AllureTest , AttachmentOptions , ContentType , ExecutableItemWrapper , isPromise , Status , StepInterface , } from "allure-js-commons" ; import { AllureReporter } from "./AllureReporter" ; import { StepWrapper } from "./StepWrapper" ; export class MochaAllure extends Allure { constructor ( private readonly reporter , runtime ) { super ( runtime ) ; } protected get currentExecutable ( ) : ExecutableItemWrapper { const executable = this . reporter . currentStep || this . reporter . currentTest || this . reporter . currentExecutable ; if ( ! executable ) { throw new Error ( "No executable!" ) ; } return executable ; } protected set currentExecutable ( executable ) { this . reporter . currentExecutable = executable ; } public step < T > ( name , body ) { const wrappedStep = this . startStep ( name ) ; let result ; try { result = wrappedStep . run ( body ) ; } catch ( err ) { wrappedStep . endStep ( ) ; throw err ; } if ( isPromise ( result ) ) { const promise = result as Promise < any > ; return promise . then ( ( a ) => { wrappedStep . endStep ( ) ; return a ; } ) . catch ( ( e ) => { wrappedStep . endStep ( ) ; throw e ; } ) ; } wrappedStep . endStep ( ) ; return result ; } logStep ( name , status ? ) { this . step ( name , ( ) => { } ) ; } public attachment ( name , content , options ) { const file = this . reporter . writeAttachment ( content , options ) ; this . currentExecutable . addAttachment ( name , options , file ) ; } public testAttachment ( name , content , options ) { const file = this . reporter . writeAttachment ( content , options ) ; this . currentTest . addAttachment ( name , options , file ) ; } protected get currentTest ( ) : AllureTest { if ( this . reporter . currentTest === null ) { throw new Error ( "No test running!" ) ; } return this . reporter . currentTest ; } private startStep ( name ) { const allureStep = this . currentExecutable . startStep ( name ) ; this . reporter . pushStep ( allureStep ) ; return new StepWrapper ( this . reporter , allureStep ) ; } }