@@ -23,6 +23,7 @@ import {
23
23
} from "miniflare" ;
24
24
import semverSatisfies from "semver/functions/satisfies.js" ;
25
25
import { createMethodsRPC } from "vitest/node" ;
26
+ import { experimental_readRawConfig } from "wrangler" ;
26
27
import { workerdBuiltinModules } from "../shared/builtin-modules" ;
27
28
import { createChunkingSocket } from "../shared/chunking-socket" ;
28
29
import { CompatibilityFlagAssertions } from "./compatibility-flag-assertions" ;
@@ -328,8 +329,37 @@ function fixupDurableObjectBindingsToSelf(
328
329
return result ;
329
330
}
330
331
332
+ function getWranglerWorkerName (
333
+ relativeWranglerConfigPath ?: string
334
+ ) : string | undefined {
335
+ if ( ! relativeWranglerConfigPath ) {
336
+ return undefined ;
337
+ }
338
+ const wranglerConfigObject = experimental_readRawConfig ( {
339
+ config : relativeWranglerConfigPath ,
340
+ } ) ;
341
+ return wranglerConfigObject . rawConfig . name ;
342
+ }
343
+
344
+ function updateWorkflowsScriptNames (
345
+ runnerWorker : WorkerOptions ,
346
+ wranglerWorkerName : string | undefined ,
347
+ testWorkerName : string
348
+ ) : void {
349
+ const workflows = runnerWorker . workflows ;
350
+ if ( ! workflows || ! wranglerWorkerName ) {
351
+ return ;
352
+ }
353
+ for ( const workflow of Object . values ( workflows ) ) {
354
+ if ( workflow . scriptName === wranglerWorkerName ) {
355
+ workflow . scriptName = testWorkerName ;
356
+ }
357
+ }
358
+ }
359
+
331
360
function fixupWorkflowBindingsToSelf (
332
- worker : SourcelessWorkerOptions
361
+ worker : SourcelessWorkerOptions ,
362
+ relativeWranglerConfigPath : string | undefined
333
363
) : Set < string > {
334
364
// TODO(someday): may need to extend this to take into account other workers
335
365
// if doing multi-worker tests across workspace projects
@@ -340,8 +370,21 @@ function fixupWorkflowBindingsToSelf(
340
370
}
341
371
for ( const key of Object . keys ( worker . workflows ) ) {
342
372
const designator = worker . workflows [ key ] ;
373
+
374
+ let workerName : string | undefined ;
375
+ // If the designator's scriptName matches its own Worker name,
376
+ // use that as the worker name, otherwise use the vitest worker's name
377
+ const wranglerWorkerName = getWranglerWorkerName (
378
+ relativeWranglerConfigPath
379
+ ) ;
380
+ if ( wranglerWorkerName && designator . scriptName === wranglerWorkerName ) {
381
+ workerName = wranglerWorkerName ;
382
+ } else {
383
+ workerName = worker . name ;
384
+ }
385
+
343
386
// `designator` hasn't been validated at this point
344
- if ( isWorkflowDesignatorToSelf ( designator , worker . name ) ) {
387
+ if ( isWorkflowDesignatorToSelf ( designator , workerName ) ) {
345
388
result . add ( designator . className ) ;
346
389
// Shallow clone to avoid mutating config
347
390
worker . workflows [ key ] = {
@@ -448,7 +491,7 @@ function buildProjectWorkerOptions(
448
491
fixupDurableObjectBindingsToSelf ( runnerWorker )
449
492
) . sort ( ) ;
450
493
const workflowClassNames = Array . from (
451
- fixupWorkflowBindingsToSelf ( runnerWorker )
494
+ fixupWorkflowBindingsToSelf ( runnerWorker , relativeWranglerConfigPath )
452
495
) . sort ( ) ;
453
496
454
497
if (
@@ -634,6 +677,17 @@ function buildProjectMiniflareOptions(
634
677
// --> single instance with single runner worker
635
678
// Multiple Workers, Isolated Storage:
636
679
// --> multiple instances each with single runner worker
680
+
681
+ // Set Workflows scriptName to the runner worker name if it matches the Wrangler worker name
682
+ const wranglerWorkerName = getWranglerWorkerName (
683
+ "project.options.wrangler?.configPath"
684
+ ) ;
685
+ updateWorkflowsScriptNames (
686
+ runnerWorker ,
687
+ wranglerWorkerName ,
688
+ runnerWorker . name
689
+ ) ;
690
+
637
691
return {
638
692
...SHARED_MINIFLARE_OPTIONS ,
639
693
inspectorPort,
@@ -653,6 +707,16 @@ function buildProjectMiniflareOptions(
653
707
testWorker . bindings = { ...testWorker . bindings } ;
654
708
testWorker . bindings [ SELF_NAME_BINDING ] = testWorker . name ;
655
709
710
+ // Set Workflows scriptName to the test worker name if it matches the Wrangler worker name
711
+ const wranglerWorkerName = getWranglerWorkerName (
712
+ project . options . wrangler ?. configPath
713
+ ) ;
714
+ updateWorkflowsScriptNames (
715
+ testWorker ,
716
+ wranglerWorkerName ,
717
+ testWorker . name
718
+ ) ;
719
+
656
720
testWorkers . push ( testWorker ) ;
657
721
}
658
722
return {
0 commit comments