import { AssertionError , ImplementationPendingError , TestCompromisedError } from '@serenity-js/core' ; import { ExecutionCompromised , ExecutionFailedWithAssertionError , ExecutionFailedWithError , ExecutionIgnored , ExecutionSkipped , ExecutionSuccessful , ImplementationPending , Outcome , } from '@serenity-js/core/lib/model' ; import { Test } from 'mocha' ; package export class MochaOutcomeMapper { public outcomeOf ( test , maybeError ? ) { const error = maybeError || test . err ; switch ( true ) { case ! ! error && this . isGoingToBeRetried ( test ) : return new ExecutionIgnored ( error ) ; case ! ! error && this . isAssertionError ( error ) : return new ExecutionFailedWithAssertionError ( error as AssertionError ) ; case ! ! error && error instanceof TestCompromisedError : return new ExecutionCompromised ( error as TestCompromisedError ) ; case ! ! error : return new ExecutionFailedWithError ( error ) ; case test . isPending ( ) && ! ! test . fn : return new ExecutionSkipped ( ) ; case test . isPending ( ) && ! test . fn : return new ImplementationPending ( new ImplementationPendingError ( `Scenario not implemented` ) ) ; default : return new ExecutionSuccessful ( ) ; } } private isGoingToBeRetried ( test ) { return ! test . isPassed ( ) && ! test . isFailed ( ) && ! test . isPending ( ) && ( test as any ) . currentRetry ( ) < test . retries ( ) ; } private isAssertionError ( error ) { return error instanceof AssertionError || this . looksLikeAnAssertionError ( error ) ; } private looksLikeAnAssertionError ( error ) { return /^AssertionError/ . test ( error . name ) && Object . prototype . hasOwnProperty . call ( error , 'expected' ) && Object . prototype . hasOwnProperty . call ( error , 'actual' ) ; } }