1- import {
2- env ,
3- introspectWorkflow ,
4- introspectWorkflowInstance ,
5- SELF ,
6- } from "cloudflare:test" ;
7- import {
8- afterEach ,
9- assert ,
10- beforeEach ,
11- describe ,
12- expect ,
13- it ,
14- vi ,
15- } from "vitest" ;
1+ import { env , introspectWorkflow , SELF } from "cloudflare:test" ;
2+ import { describe , expect , it , vi } from "vitest" ;
3+
4+ const STATUS_COMPLETE = "complete" ;
165
176describe ( "Test Workflow" , ( ) => {
187 it ( "should be able to trigger a workflow" , async ( ) => {
@@ -30,78 +19,78 @@ describe("Test Workflow", () => {
3019 const res = await SELF . fetch ( `https://mock-worker.local?id=${ json . id } ` ) ;
3120
3221 const statusJson = await res . json < { status : string } > ( ) ;
33- console . log ( statusJson ) ;
34- return statusJson . status === "complete" ;
22+ expect ( statusJson . status ) . toBe ( STATUS_COMPLETE ) ;
23+ return true ;
3524 } , 1000 ) ;
3625 } ) ;
3726
3827 it ( "workflow should reach the end and be successful with introspector" , async ( ) => {
39- const introspector = await introspectWorkflow ( env . TEST_WORKFLOW ) ;
28+ // CONFIG with `using` to ensure cleanup:
29+ await using introspector = await introspectWorkflow ( env . TEST_WORKFLOW ) ;
4030
4131 await SELF . fetch ( "https://mock-worker.local" ) ;
4232
4333 const instances = introspector . get ( ) ;
44- assert ( instances . length === 1 ) ;
34+ expect ( instances . length ) . toBe ( 1 ) ;
4535
36+ // ASSERTIONS:
4637 const instance = instances [ 0 ] ;
47- await instance . waitForStatus ( "complete" ) ;
48- await instance . cleanUp ( ) ;
38+ await instance . waitForStatus ( STATUS_COMPLETE ) ;
39+
40+ // CLEANUP: assured by Symbol.asyncDispose
4941 } ) ;
5042} ) ;
5143
5244describe ( "Test long Workflow" , ( ) => {
5345 const STEP_NAME = "my step" ;
5446 const mockResult = "mocked result" ;
5547
56- let introspector : Awaited < ReturnType < typeof introspectWorkflow > > ;
57- let instances : Awaited < ReturnType < typeof introspectWorkflowInstance > > [ ] ;
58-
59- beforeEach ( async ( ) => {
60- // CONFIG:
61- introspector = await introspectWorkflow ( env . TEST_LONG_WORKFLOW ) ;
48+ it ( "workflow should be able to introspect and reach the end and be successful" , async ( ) => {
49+ // CONFIG with `using` to ensure cleanup:
50+ await using introspector = await introspectWorkflow ( env . TEST_LONG_WORKFLOW ) ;
6251 introspector . modifyAll ( async ( m ) => {
6352 await m . disableSleeps ( ) ;
6453 await m . mockStepResult ( { name : STEP_NAME } , mockResult ) ;
6554 } ) ;
66- } ) ;
67-
68- afterEach ( async ( ) => {
69- // CLEANUP:
70- // Instance introspectors should be clean to prevent persisted state across tests
71- for ( const instance of instances ) {
72- instance . cleanUp ( ) ;
73- }
74-
75- // Workflow introspector should be cleaned at the end of/after each test
76- introspector . cleanUp ( ) ;
77- } ) ;
7855
79- it ( "workflow should be able to introspect and reach the end and be successful" , async ( ) => {
8056 await SELF . fetch ( "https://mock-worker.local/long-workflow" ) ;
8157
82- instances = introspector . get ( ) ;
83- assert ( instances . length === 1 ) ;
58+ const instances = introspector . get ( ) ;
59+ expect ( instances . length ) . toBe ( 1 ) ;
8460
8561 // ASSERTIONS:
8662 const instance = instances [ 0 ] ;
8763 expect ( await instance . waitForStepResult ( { name : STEP_NAME } ) ) . toEqual (
8864 mockResult
8965 ) ;
90- await instance . waitForStatus ( "complete" ) ;
66+ await instance . waitForStatus ( STATUS_COMPLETE ) ;
67+
68+ // CLEANUP: done by Symbol.asyncDispose
9169 } ) ;
9270
9371 it ( "workflow batch should be able to introspect and reach the end and be successful" , async ( ) => {
72+ let introspector = await introspectWorkflow ( env . TEST_LONG_WORKFLOW ) ;
73+ introspector . modifyAll ( async ( m ) => {
74+ await m . disableSleeps ( ) ;
75+ await m . mockStepResult ( { name : STEP_NAME } , mockResult ) ;
76+ } ) ;
77+
9478 await SELF . fetch ( "https://mock-worker.local/long-workflow-batch" ) ;
9579
96- instances = introspector . get ( ) ;
97- assert ( instances . length === 3 ) ;
80+ const instances = introspector . get ( ) ;
81+ expect ( instances . length ) . toBe ( 3 ) ;
9882
9983 // ASSERTIONS:
10084 for ( const instance of instances ) {
10185 expect ( await instance . waitForStepResult ( { name : STEP_NAME } ) ) . toEqual (
10286 mockResult
10387 ) ;
104- await instance . waitForStatus ( "complete" ) ;
88+ await instance . waitForStatus ( STATUS_COMPLETE ) ;
10589 }
90+
91+ // CLEANUP:
92+ // Workflow introspector should be cleaned at the end of/after each test, if no `using` keyword is used for the introspector
93+ // Cleans up all intercepted instances
94+ introspector . cleanUp ( ) ;
10695 } ) ;
10796} ) ;
0 commit comments