Skip to content

Commit 68a9008

Browse files
authored
Don't crash Vitest when started with a non-existent tail consumer (#9350)
* Don't crash Vitest when started with a non-existent tail consumer * Create warm-dingos-lay.md
1 parent 7ddd865 commit 68a9008

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

.changeset/warm-dingos-lay.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+
Don't crash Vitest when started with a non-existent tail consumer

fixtures/vitest-pool-workers-examples/multiple-workers/api-service/wrangler.jsonc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
"service": "database-service",
1313
},
1414
],
15+
"tail_consumers": [
16+
{ "service": "this-tail-does-not-exist" },
17+
{ "service": "tail-consumer" },
18+
],
1519
}

fixtures/vitest-pool-workers-examples/multiple-workers/vitest.config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ export default defineWorkersProject({
9494
compatibilityFlags: ["nodejs_compat"],
9595
kvNamespaces: ["KV_NAMESPACE"],
9696
},
97+
{
98+
name: "tail-consumer",
99+
modules: [
100+
{
101+
path: "index.js",
102+
type: "ESModule",
103+
contents: /* javascript */ `
104+
export default {
105+
tail(event) {
106+
console.log("tail event received")
107+
}
108+
}
109+
`,
110+
},
111+
],
112+
compatibilityDate: "2024-01-01",
113+
},
97114
],
98115
},
99116
},

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,38 @@ function parseWorkerOptions(
139139

140140
const log = new Log(LogLevel.WARN, { prefix: "vpw" });
141141

142+
function filterTails(
143+
tails: WorkerOptions["tails"],
144+
userWorkers?: { name?: string }[]
145+
) {
146+
// Only connect the tail consumers that represent Workers that are defined in the Vitest config. Warn that a tail will be omitted otherwise
147+
// This _differs from service bindings_ because tail consumers are "optional" in a sense, and shouldn't affect the runtime behaviour of a Worker
148+
return tails?.filter((tailService) => {
149+
let name: string;
150+
if (typeof tailService === "string") {
151+
name = tailService;
152+
} else if (
153+
typeof tailService === "object" &&
154+
"name" in tailService &&
155+
typeof tailService.name === "string"
156+
) {
157+
name = tailService.name;
158+
} else {
159+
// Don't interfere with network-based tail connections (e.g. via the dev registry), or kCurrentWorker
160+
return true;
161+
}
162+
const found = userWorkers?.some((w) => w.name === name);
163+
164+
if (!found) {
165+
log.warn(
166+
`Tail consumer "${name}" was not found in your config. Make sure you add it if you'd like to simulate receiving tail events locally.`
167+
);
168+
}
169+
170+
return found;
171+
});
172+
}
173+
142174
async function parseCustomPoolOptions(
143175
rootPath: string,
144176
value: unknown,
@@ -243,6 +275,11 @@ async function parseCustomPoolOptions(
243275
options.miniflare as SourcelessWorkerOptions
244276
);
245277

278+
options.miniflare = {
279+
...options.miniflare,
280+
tails: filterTails(workerOptions.tails, options.miniflare.workers),
281+
};
282+
246283
// Record any Wrangler `define`s
247284
options.defines = define;
248285
}

0 commit comments

Comments
 (0)