import { Allure , ContentType , Severity } from "allure-js-commons" ; import { processDescriptor } from "./descriptor" ; type TestDecorator = ( target : unknown , property : string , descriptor : PropertyDescriptor ) => PropertyDescriptor ; const enum LinkType { ISSUE = "issue" , TMS = "tms" } const defaultTrackerAddress = "http://localhost" ; let allure ; let pmsUrl ; let tmsUrl ; export const step = < T > ( nameFn ) => { return ( target , propertyKey , descriptor ) => { const original = descriptor . value ; let callable ; if ( typeof original === "function" ) { descriptor . value = function ( ... args ) { try { const value = typeof nameFn === "function" ? nameFn . apply ( this , args ) : nameFn ; callable = ( ) => getAllure ( ) . step ( value , ( ) => original . apply ( this , args ) ) ; } catch ( e ) { console . error ( `[ERROR] Failed to apply step decorator: ${ e }` ) ; } return callable ? callable . apply ( this , args ) : original . apply ( this , args ) ; } ; } return descriptor ; } ; } ; export const attachment = < T > ( name , type ) => { return ( target , propertyKey , descriptor ) => { const original = descriptor . value ; let callable ; if ( typeof original === "function" ) { descriptor . value = function ( ... args ) { try { const content = original . apply ( this , args ) ; callable = ( ) => getAllure ( ) . attachment ( name , content , type ) ; } catch ( e ) { console . error ( `[ERROR] Failed to apply attachment decorator: ${ e }` ) ; } return callable ? callable . apply ( this , args ) : { } ; } ; } return descriptor ; } ; } ; export const testCaseId = < T > ( idFn ) : TestDecorator => { return processDecorator ( idFn , ( id ) => getAllure ( ) . link ( `${ getTmsUrl ( ) }/${ id }` , id , LinkType . TMS ) ) ; } ; export const issue = < T > ( idFn ) : TestDecorator => { return processDecorator ( idFn , ( id ) => getAllure ( ) . link ( `${ getPmsUrl ( ) }/${ id }` , id , LinkType . ISSUE ) ) ; } ; export const feature = < T > ( featureFn ) : TestDecorator => { return processDecorator ( featureFn , ( name ) => getAllure ( ) . feature ( name ) ) ; } ; export const story = < T > ( storyFn ) : TestDecorator => { return processDecorator ( storyFn , ( name ) => getAllure ( ) . story ( name ) ) ; } ; export const severity = < T > ( severityFn ) : TestDecorator => { return processDecorator ( severityFn , ( name ) => getAllure ( ) . severity ( name ) ) ; } ; export const tag = < T > ( tagFn ) => { return processDecorator ( tagFn , ( name ) => getAllure ( ) . tag ( name ) ) ; } ; export const owner = < T > ( ownerFn ) => { return processDecorator ( ownerFn , ( name ) => getAllure ( ) . owner ( name ) ) ; } ; export const epic = < T > ( epicFn ) => { return processDecorator ( epicFn , ( name ) => getAllure ( ) . epic ( name ) ) ; } ; export const description = < T > ( descriptionFn ) => { return processDecorator ( descriptionFn , ( text ) => getAllure ( ) . description ( text ) ) ; } ; export const decorate = < T extends Allure > ( allureInstance ) : void => { allure = allureInstance ; } ; export const getAllure = < T extends Allure > ( ) : T => { if ( ! allure ) { throw new Error ( "Unable to find Allure implementation" ) ; } return allure as T ; } ; export const assignTmsUrl = ( url ) : void => { tmsUrl = url ; } ; const getTmsUrl = ( ) : string => { return tmsUrl || defaultTrackerAddress ; } ; export const assignPmsUrl = ( url ) : void => { pmsUrl = url ; } ; const getPmsUrl = ( ) : string => { return pmsUrl || defaultTrackerAddress ; } ; const processDecorator = < T > ( parameterFn , reporterFn ) : TestDecorator => { return ( target , property , descriptor ) => processDescriptor ( parameterFn , reporterFn , descriptor ) ; } ;