Skip to content

Commit 6cae13a

Browse files
authored
fix(vitest-pool-workers): fetchMock support on auxiliary worker (#8239)
1 parent 1ad53c5 commit 6cae13a

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

.changeset/huge-pears-tickle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"miniflare": patch
3+
---
4+
5+
fix: allow the `fetchMock` option to be parsed upfront before passing it to Miniflare

packages/miniflare/src/plugins/core/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ export const CoreOptionsSchema = CoreOptionsSchemaInput.transform((value) => {
163163
"Only one of `outboundService` or `fetchMock` may be specified per worker"
164164
);
165165
}
166+
167+
// The `fetchMock` option is used to construct the `outboundService` only
168+
// Removing it from the output allows us to re-parse the options later
169+
// This allows us to validate the options and then feed them into Miniflare without issue.
170+
value.fetchMock = undefined;
166171
value.outboundService = (req) => fetch(req, { dispatcher: fetchMock });
167172
}
168173
return value;

packages/miniflare/test/index.spec.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import {
3333
Miniflare,
3434
MiniflareCoreError,
3535
MiniflareOptions,
36+
parseWithRootPath,
37+
PLUGINS,
3638
ReplaceWorkersTypes,
3739
Response,
3840
viewToBuffer,
@@ -1108,15 +1110,27 @@ test("Miniflare: fetch mocking", async (t) => {
11081110
const origin = fetchMock.get("https://example.com");
11091111
origin.intercept({ method: "GET", path: "/" }).reply(200, "Mocked response!");
11101112

1111-
const mf = new Miniflare({
1113+
const mfOptions: MiniflareOptions = {
11121114
modules: true,
11131115
script: `export default {
11141116
async fetch() {
11151117
return fetch("https://example.com/");
11161118
}
11171119
}`,
11181120
fetchMock,
1119-
});
1121+
};
1122+
const resultOptions = {} as MiniflareOptions;
1123+
1124+
// Verify that options with `fetchMock` can be parsed first before passing to Miniflare
1125+
// Regression test for https://github.com/cloudflare/workers-sdk/issues/5486
1126+
for (const plugin of Object.values(PLUGINS)) {
1127+
Object.assign(
1128+
resultOptions,
1129+
parseWithRootPath("", plugin.options, mfOptions)
1130+
);
1131+
}
1132+
1133+
const mf = new Miniflare(resultOptions);
11201134
t.teardown(() => mf.dispose());
11211135
const res = await mf.dispatchFetch("http://localhost");
11221136
t.is(await res.text(), "Mocked response!");

0 commit comments

Comments
 (0)