1- import { Modules } from "@medusajs/framework/utils"
1+ import { Modules , TransactionState } from "@medusajs/framework/utils"
22import {
33 createStep ,
44 createWorkflow ,
@@ -11,8 +11,10 @@ import {
1111 adminHeaders ,
1212 createAdminUser ,
1313} from "../../../helpers/create-admin-user"
14+ import { emitEventStep } from "@medusajs/core-flows"
15+ import { IEventBusModuleService } from "@medusajs/types"
1416
15- jest . setTimeout ( 50000 )
17+ jest . setTimeout ( 300000 )
1618
1719medusaIntegrationTestRunner ( {
1820 testSuite : ( { dbConnection, getContainer, api } ) => {
@@ -88,6 +90,81 @@ medusaIntegrationTestRunner({
8890 )
8991 } )
9092 } )
93+
94+ describe ( "Workflows event" , ( ) => {
95+ const failingEventName = "failing-event"
96+
97+ it ( "should not compensate the workflow if the event subscriber fails" , async ( ) => {
98+ const step1 = createStep (
99+ {
100+ name : "my-step" ,
101+ } ,
102+ async ( _ ) => {
103+ return new StepResponse ( { result : "success" } )
104+ }
105+ )
106+
107+ createWorkflow (
108+ {
109+ name : "my-workflow-name" ,
110+ retentionTime : 50 ,
111+ } ,
112+ function ( input : WorkflowData < { initial : string } > ) {
113+ const stepRes = step1 ( )
114+
115+ emitEventStep ( {
116+ eventName : failingEventName ,
117+ data : {
118+ input : stepRes ,
119+ } ,
120+ } )
121+
122+ return new WorkflowResponse ( stepRes )
123+ }
124+ )
125+
126+ const container = getContainer ( )
127+ const eventBus = container . resolve (
128+ Modules . EVENT_BUS
129+ ) as IEventBusModuleService
130+
131+ const eventSpy = jest . fn ( )
132+ eventBus . subscribe ( failingEventName , async ( event ) => {
133+ eventSpy ( event )
134+ throw new Error ( "Failed to emit event" )
135+ } )
136+
137+ const engine = container . resolve ( Modules . WORKFLOW_ENGINE )
138+
139+ const transactionId = "trx-id-failing-event"
140+ const res = await engine . run ( "my-workflow-name" , {
141+ transactionId,
142+ input : {
143+ initial : "abc" ,
144+ } ,
145+ } )
146+
147+ expect ( res . result ) . toEqual ( { result : "success" } )
148+
149+ const executions = await engine . listWorkflowExecutions ( {
150+ transaction_id : transactionId ,
151+ } )
152+
153+ expect ( executions . length ) . toBe ( 1 )
154+ expect ( executions [ 0 ] . state ) . toBe ( TransactionState . DONE )
155+
156+ expect ( eventSpy ) . toHaveBeenCalledTimes ( 1 )
157+ expect ( eventSpy ) . toHaveBeenCalledWith (
158+ expect . objectContaining ( {
159+ data : {
160+ input : {
161+ result : "success" ,
162+ } ,
163+ } ,
164+ } )
165+ )
166+ } )
167+ } )
91168 } )
92169 } ,
93170} )
0 commit comments