From 6a110217c546b6033b5ad939cb48a69c2bcd104e Mon Sep 17 00:00:00 2001
From: emily-shen <69125074+emily-shen@users.noreply.github.com>
Date: Wed, 29 Jan 2025 11:00:40 +0000
Subject: [PATCH] fixup and test
---
.../workers-assets-run-worker-first/README.md | 3 +++
.../public/binding.html | 1 +
.../public/index.html | 11 +++++++++
.../src/env.d.ts | 3 +++
.../src/index.ts | 11 +++++++++
.../src/tsconfig.json | 4 ++++
.../test/assets.test.ts | 23 +++++++++++++++++++
.../test/env.d.ts | 3 +++
.../test/tsconfig.json | 4 ++++
.../tsconfig.json | 4 ++++
.../vitest.config.ts | 11 +++++++++
.../wrangler.toml | 11 +++++++++
.../vitest-pool-workers/src/pool/config.ts | 11 ++++-----
13 files changed, 93 insertions(+), 7 deletions(-)
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/README.md
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/public/binding.html
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/public/index.html
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/env.d.ts
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/index.ts
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/tsconfig.json
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/assets.test.ts
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/env.d.ts
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/tsconfig.json
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/tsconfig.json
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/vitest.config.ts
create mode 100644 fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/wrangler.toml
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/README.md b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/README.md
new file mode 100644
index 000000000000..194a8fc0afa5
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/README.md
@@ -0,0 +1,3 @@
+# ✅ workers-static-assets-with-user-worker-run-first
+
+This Worker contains assets as well as a Worker script. `run_worker_first` is set to `true`.
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/public/binding.html b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/public/binding.html
new file mode 100644
index 000000000000..7f4f36ea8afe
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/public/binding.html
@@ -0,0 +1 @@
+
binding.html
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/public/index.html b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/public/index.html
new file mode 100644
index 000000000000..29df18baca97
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/public/index.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+ Hello, World!
+
+
+ Asset index.html
+
+
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/env.d.ts b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/env.d.ts
new file mode 100644
index 000000000000..079bf105b5ab
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/env.d.ts
@@ -0,0 +1,3 @@
+interface Env {
+ ASSETS: Fetcher;
+}
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/index.ts b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/index.ts
new file mode 100644
index 000000000000..60abf08a1ee1
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/index.ts
@@ -0,0 +1,11 @@
+export default {
+ async fetch(request, env, ctx): Promise {
+ const url = new URL(request.url);
+ if (url.pathname === "/binding") {
+ return env.ASSETS.fetch(request);
+ }
+ return new Response("Hello, World!", {
+ headers: { "x-test": "hello" },
+ });
+ },
+} satisfies ExportedHandler;
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/tsconfig.json b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/tsconfig.json
new file mode 100644
index 000000000000..0141323e2fc0
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/src/tsconfig.json
@@ -0,0 +1,4 @@
+{
+ "extends": "../../tsconfig.workerd.json",
+ "include": ["./**/*.ts"]
+}
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/assets.test.ts b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/assets.test.ts
new file mode 100644
index 000000000000..a732eddf556a
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/assets.test.ts
@@ -0,0 +1,23 @@
+import { SELF } from "cloudflare:test";
+import { describe, expect, it } from "vitest";
+
+describe("Hello World user worker", () => {
+ describe("integration test style", async () => {
+ it('responds with "Hello, World!" (integration style)', async () => {
+ const response = await SELF.fetch("http://example.com/message");
+ expect(await response.text()).toMatchInlineSnapshot(`"Hello, World!"`);
+
+ expect(response.headers.get("x-test")).toEqual("hello");
+ });
+ it("does NOT get assets directly, but always hits the user Worker", async () => {
+ // asset at /index.html
+ const response = await SELF.fetch("http://example.com/index.html");
+ expect(await response.text()).toContain("Hello, World!");
+ expect(response.headers.get("x-test")).toEqual("hello");
+ });
+ it("can still get assets via binding", async () => {
+ const response = await SELF.fetch("http://example.com/binding");
+ expect(await response.text()).toContain("binding.html");
+ });
+ });
+});
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/env.d.ts b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/env.d.ts
new file mode 100644
index 000000000000..67b3610dbc7d
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/env.d.ts
@@ -0,0 +1,3 @@
+declare module "cloudflare:test" {
+ interface ProvidedEnv extends Env {}
+}
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/tsconfig.json b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/tsconfig.json
new file mode 100644
index 000000000000..49d66320eafc
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/test/tsconfig.json
@@ -0,0 +1,4 @@
+{
+ "extends": "../../tsconfig.workerd-test.json",
+ "include": ["./**/*.ts", "../src/env.d.ts"]
+}
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/tsconfig.json b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/tsconfig.json
new file mode 100644
index 000000000000..90e58bf03ef0
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/tsconfig.json
@@ -0,0 +1,4 @@
+{
+ "extends": "../tsconfig.node.json",
+ "include": ["./*.ts"]
+}
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/vitest.config.ts b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/vitest.config.ts
new file mode 100644
index 000000000000..3f34c9327a9d
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/vitest.config.ts
@@ -0,0 +1,11 @@
+import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config";
+
+export default defineWorkersProject({
+ test: {
+ poolOptions: {
+ workers: {
+ wrangler: { configPath: "./wrangler.toml" },
+ },
+ },
+ },
+});
diff --git a/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/wrangler.toml b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/wrangler.toml
new file mode 100644
index 000000000000..c809a4f5244f
--- /dev/null
+++ b/fixtures/vitest-pool-workers-examples/workers-assets-run-worker-first/wrangler.toml
@@ -0,0 +1,11 @@
+#:schema node_modules/wrangler/config-schema.json
+name = "workers-static-assets-with-user-worker-run-first"
+main = "src/index.ts"
+compatibility_date = "2024-09-19"
+
+[assets]
+directory = "./public"
+binding = "ASSETS"
+html_handling = "auto-trailing-slash"
+not_found_handling = "none"
+run_worker_first = true
diff --git a/packages/vitest-pool-workers/src/pool/config.ts b/packages/vitest-pool-workers/src/pool/config.ts
index 4548781e30fe..8e1e4f726656 100644
--- a/packages/vitest-pool-workers/src/pool/config.ts
+++ b/packages/vitest-pool-workers/src/pool/config.ts
@@ -218,13 +218,10 @@ async function parseCustomPoolOptions(
if (options.miniflare?.assets) {
// (Used to set the SELF binding to point to the router worker instead)
options.miniflare.hasAssetsAndIsVitest = true;
- options.miniflare.assets = {
- ...options.miniflare.assets,
- routingConfig: {
- ...options.miniflare.assets.routingConfig,
- has_user_worker: Boolean(options.main),
- },
- };
+ options.miniflare.assets.routingConfig ??= {};
+ options.miniflare.assets.routingConfig.has_user_worker = Boolean(
+ options.main
+ );
}
return options;
}