import { EnvelopeListener , ITestCase , ITestStep , IWorld } from './types' ; import * as messages from '@cucumber/messages' ; import IClock from './IClock' ; import { getWorstTestStepResult } from '@cucumber/messages' ; const { millisecondsSinceEpochToTimestamp } = messages . TimeConversion ; export default class TestCase implements ITestCase { constructor ( public readonly id , private readonly testSteps , private readonly pickleId , private readonly clock ) { } public toMessage ( ) { return { testCase : { id : this . id , pickleId : this . pickleId , testSteps : this . testSteps . map ( ( step ) => step . toMessage ( ) ) , } , } ; } public async execute ( listener , attempt , retryable , testCaseStartedId ) { listener ( { testCaseStarted : { attempt , testCaseId : this . id , id : testCaseStartedId , timestamp : millisecondsSinceEpochToTimestamp ( this . clock . clockNow ( ) ) , } , } ) ; const world = { attach : ( ) => { throw new Error ( 'Attach is not ready' ) ; } , log : ( ) => { throw new Error ( 'Log is not ready' ) ; } , } ; const testStepResults = [ ] ; let previousPassed = true ; for ( const testStep of this . testSteps ) { const testStepResult = await testStep . execute ( world , testCaseStartedId , listener , previousPassed ) ; previousPassed = testStepResult . status === messages . TestStepResultStatus . PASSED ; testStepResults . push ( testStepResult ) ; } const willBeRetried = retryable && getWorstTestStepResult ( testStepResults ) . status === messages . TestStepResultStatus . FAILED ; listener ( { testCaseFinished : { testCaseStartedId : testCaseStartedId , timestamp : millisecondsSinceEpochToTimestamp ( this . clock . clockNow ( ) ) , willBeRetried , } , } ) ; const finalStepResult = getWorstTestStepResult ( testStepResults ) ; return finalStepResult . status ; } }