Skip to content

Commit f50ed8f

Browse files
committed
fix: make vitest-pool-workers not break if Workflow binding's script_name matches its own Worker name
removing try catch since error gets thrown either way use userConfigPath instead of redirected one (#10005) Revert "use userConfigPath instead of redirected one (#10005)" This reverts commit 5c11fe7. fix deep import prettier run fix: make vitest-pool-workers not break if Workflow binding's script_name matches its own Worker name
1 parent 26ffa05 commit f50ed8f

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

.changeset/spotty-pants-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
Make vitest-pool-workers not break when a Workflow binding script_name matches its Worker name

packages/vitest-pool-workers/src/pool/index.ts

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from "miniflare";
2424
import semverSatisfies from "semver/functions/satisfies.js";
2525
import { createMethodsRPC } from "vitest/node";
26+
import { experimental_readRawConfig } from "wrangler";
2627
import { workerdBuiltinModules } from "../shared/builtin-modules";
2728
import { createChunkingSocket } from "../shared/chunking-socket";
2829
import { CompatibilityFlagAssertions } from "./compatibility-flag-assertions";
@@ -328,8 +329,37 @@ function fixupDurableObjectBindingsToSelf(
328329
return result;
329330
}
330331

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+
331360
function fixupWorkflowBindingsToSelf(
332-
worker: SourcelessWorkerOptions
361+
worker: SourcelessWorkerOptions,
362+
relativeWranglerConfigPath: string | undefined
333363
): Set<string> {
334364
// TODO(someday): may need to extend this to take into account other workers
335365
// if doing multi-worker tests across workspace projects
@@ -340,8 +370,21 @@ function fixupWorkflowBindingsToSelf(
340370
}
341371
for (const key of Object.keys(worker.workflows)) {
342372
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+
343386
// `designator` hasn't been validated at this point
344-
if (isWorkflowDesignatorToSelf(designator, worker.name)) {
387+
if (isWorkflowDesignatorToSelf(designator, workerName)) {
345388
result.add(designator.className);
346389
// Shallow clone to avoid mutating config
347390
worker.workflows[key] = {
@@ -448,7 +491,7 @@ function buildProjectWorkerOptions(
448491
fixupDurableObjectBindingsToSelf(runnerWorker)
449492
).sort();
450493
const workflowClassNames = Array.from(
451-
fixupWorkflowBindingsToSelf(runnerWorker)
494+
fixupWorkflowBindingsToSelf(runnerWorker, relativeWranglerConfigPath)
452495
).sort();
453496

454497
if (
@@ -634,6 +677,17 @@ function buildProjectMiniflareOptions(
634677
// --> single instance with single runner worker
635678
// Multiple Workers, Isolated Storage:
636679
// --> 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+
637691
return {
638692
...SHARED_MINIFLARE_OPTIONS,
639693
inspectorPort,
@@ -653,6 +707,16 @@ function buildProjectMiniflareOptions(
653707
testWorker.bindings = { ...testWorker.bindings };
654708
testWorker.bindings[SELF_NAME_BINDING] = testWorker.name;
655709

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+
656720
testWorkers.push(testWorker);
657721
}
658722
return {

0 commit comments

Comments
 (0)