Skip to content

Commit

Permalink
fixup and test
Browse files Browse the repository at this point in the history
  • Loading branch information
emily-shen committed Jan 29, 2025
1 parent ca2f79b commit 6a11021
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>binding.html</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hello, World!</title>
</head>
<body>
<h1>Asset index.html</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface Env {
ASSETS: Fetcher;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default {
async fetch(request, env, ctx): Promise<Response> {
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<Env>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.workerd.json",
"include": ["./**/*.ts"]
}
Original file line number Diff line number Diff line change
@@ -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");
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module "cloudflare:test" {
interface ProvidedEnv extends Env {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.workerd-test.json",
"include": ["./**/*.ts", "../src/env.d.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.node.json",
"include": ["./*.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config";

export default defineWorkersProject({
test: {
poolOptions: {
workers: {
wrangler: { configPath: "./wrangler.toml" },
},
},
},
});
Original file line number Diff line number Diff line change
@@ -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
11 changes: 4 additions & 7 deletions packages/vitest-pool-workers/src/pool/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 6a11021

Please sign in to comment.