import { WorkflowClient , WorkflowFailedError , ActivityFailure , ApplicationFailure } from '@temporalio/client' ; import { Core , Worker , DefaultLogger } from '@temporalio/worker' ; import { describe , before , after , afterEach , it } from 'mocha' ; import assert from 'assert' ; import axios from 'axios' ; import sinon from 'sinon' ; import { httpWorkflow } from '../workflows' ; import * as activities from '../activities' ; describe ( 'example workflow' , function ( ) { let shutdown ; let execute ; this . slow ( 1000 ) ; before ( async function ( ) { this . timeout ( 10 * 1000 ) ; await Core . install ( { logger : new DefaultLogger ( 'WARN' ) } ) ; const worker = await Worker . create ( { taskQueue : 'test-activities' , workflowsPath : require . resolve ( '../workflows' ) , activities , } ) ; const runPromise = worker . run ( ) ; shutdown = async ( ) => { worker . shutdown ( ) ; await runPromise ; } ; } ) ; beforeEach ( ( ) => { const client = new WorkflowClient ( ) ; execute = ( ) => client . execute ( httpWorkflow , { taskQueue : 'test-activities' , workflowExecutionTimeout : 1000 , workflowId : `test-${ Date . now ( ) }` , } ) ; } ) ; after ( async ( ) => { await shutdown ( ) ; } ) ; afterEach ( ( ) => { sinon . restore ( ) ; } ) ; it ( 'returns correct result' , async ( ) => { const result = await execute ( ) ; assert . equal ( result , 'The answer is 42' ) ; } ) ; it ( 'retries one failure' , async ( ) => { let numCalls = 0 ; sinon . stub ( axios , 'get' ) . callsFake ( ( ) => { if ( numCalls ++ === 0 ) { return Promise . reject ( new Error ( 'first error' ) ) ; } return Promise . resolve ( { data : { args : { answer : '88' } } } ) ; } ) ; const result = await execute ( ) ; assert . equal ( result , 'The answer is 88' ) ; } ) ; it ( 'bubbles up activity errors' , async ( ) => { sinon . stub ( axios , 'get' ) . callsFake ( ( ) => Promise . reject ( new Error ( 'example error' ) ) ) ; await assert . rejects ( execute , ( err ) => err instanceof WorkflowFailedError && err . cause instanceof ActivityFailure && err . cause . cause instanceof ApplicationFailure && err . cause . cause . message === 'example error' ) ; } ) ; } ) ;