-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add workers assets to vitest integration (#6835)
* add worker with static assets to vitest fixtures * it! works! (add miniflare/vitest plumbing) * add assets options to miniflare README.md * add assets only vitest fixture * changeset * add assets to unstable_getMiniflareWorkerOptions * add todo for rpc entrypoint self
- Loading branch information
1 parent
54924a4
commit 5c50949
Showing
30 changed files
with
285 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"miniflare": patch | ||
--- | ||
|
||
fix: rename asset plugin options slightly to match wrangler.toml better | ||
|
||
Renamed `path` -> `directory`, `bindingName` -> `binding`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@cloudflare/vitest-pool-workers": patch | ||
--- | ||
|
||
feature: enable asset routing in the vitest integration for Workers + static assets | ||
|
||
Integration tests (using the SELF binding) in Workers + assets projects will now return static assets if present on that path. Previously all requests went to the user worker regardless of whether static assets would have been served in production. | ||
|
||
Unit style tests intentionally do not return static assets. |
5 changes: 5 additions & 0 deletions
5
fixtures/vitest-pool-workers-examples/workers-assets-only/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# ✅ workers-with-assets-only | ||
|
||
This example contains assets without a Worker script. | ||
|
||
An asset-only project can only be tested integration-style using the SELF binding, as there is no Worker to import and unit test. |
11 changes: 11 additions & 0 deletions
11
fixtures/vitest-pool-workers-examples/workers-assets-only/public/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
13 changes: 13 additions & 0 deletions
13
fixtures/vitest-pool-workers-examples/workers-assets-only/test/assets-only.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { env, SELF } from "cloudflare:test"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
// There is no Worker so we can't import one and unit test | ||
it("can test asset serving (integration style)", async () => { | ||
let response = await SELF.fetch("http://example.com/index.html"); | ||
expect(await response.text()).toContain("Asset index.html"); | ||
|
||
// no such asset | ||
response = await SELF.fetch("http://example.com/message"); | ||
expect(await response.text()).toBeFalsy(); | ||
expect(response.status).toBe(404); | ||
}); |
4 changes: 4 additions & 0 deletions
4
fixtures/vitest-pool-workers-examples/workers-assets-only/test/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.workerd-test.json", | ||
"include": ["./**/*.ts"] | ||
} |
4 changes: 4 additions & 0 deletions
4
fixtures/vitest-pool-workers-examples/workers-assets-only/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../tsconfig.node.json", | ||
"include": ["./*.ts"] | ||
} |
11 changes: 11 additions & 0 deletions
11
fixtures/vitest-pool-workers-examples/workers-assets-only/vitest.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
}, | ||
}, | ||
}, | ||
}); |
8 changes: 8 additions & 0 deletions
8
fixtures/vitest-pool-workers-examples/workers-assets-only/wrangler.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#:schema node_modules/wrangler/config-schema.json | ||
name = "workers-static-assets-only" | ||
compatibility_date = "2024-09-19" | ||
compatibility_flags = ["nodejs_compat"] | ||
|
||
[assets] | ||
directory = "./public" | ||
html_handling = "none" |
5 changes: 5 additions & 0 deletions
5
fixtures/vitest-pool-workers-examples/workers-assets/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# ✅ workers-static-assets-with-user-worker | ||
|
||
This Worker contains assets as well as a Worker script | ||
|
||
Integration tests should hit assets, but unit tests which directly import the Worker should not. |
1 change: 1 addition & 0 deletions
1
fixtures/vitest-pool-workers-examples/workers-assets/public/binding.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>binding.html</p> |
11 changes: 11 additions & 0 deletions
11
fixtures/vitest-pool-workers-examples/workers-assets/public/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
3 changes: 3 additions & 0 deletions
3
fixtures/vitest-pool-workers-examples/workers-assets/src/env.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
interface Env { | ||
ASSETS: Fetcher; | ||
} |
15 changes: 15 additions & 0 deletions
15
fixtures/vitest-pool-workers-examples/workers-assets/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default { | ||
async fetch(request, env, ctx): Promise<Response> { | ||
const url = new URL(request.url); | ||
switch (url.pathname) { | ||
case "/message": | ||
return new Response("Hello, World!"); | ||
case "/random": | ||
return new Response(crypto.randomUUID()); | ||
case "/binding": | ||
return env.ASSETS.fetch(request); | ||
default: | ||
return new Response(null, { status: 404 }); | ||
} | ||
}, | ||
} satisfies ExportedHandler<Env>; |
4 changes: 4 additions & 0 deletions
4
fixtures/vitest-pool-workers-examples/workers-assets/src/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.workerd.json", | ||
"include": ["./**/*.ts"] | ||
} |
57 changes: 57 additions & 0 deletions
57
fixtures/vitest-pool-workers-examples/workers-assets/test/assets.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
createExecutionContext, | ||
env, | ||
SELF, | ||
waitOnExecutionContext, | ||
} from "cloudflare:test"; | ||
import { describe, expect, it } from "vitest"; | ||
import worker from "../src"; | ||
|
||
// This will improve in the next major version of `@cloudflare/workers-types`, | ||
// but for now you'll need to do something like this to get a correctly-typed | ||
// `Request` to pass to `createPagesEventContext()`. | ||
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>; | ||
|
||
describe("Hello World user worker", () => { | ||
describe("unit test style", async () => { | ||
it('responds with "Hello, World!', async () => { | ||
const request = new IncomingRequest("http://example.com/message"); | ||
// Create an empty context to pass to `worker.fetch()`. | ||
const ctx = createExecutionContext(); | ||
const response = await worker.fetch(request, env, ctx); | ||
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions | ||
await waitOnExecutionContext(ctx); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello, World!"`); | ||
}); | ||
it("does not get assets directly if importing Worker directly", async () => { | ||
const request = new IncomingRequest("http://example.com/"); | ||
const ctx = createExecutionContext(); | ||
const response = await worker.fetch(request, env, ctx); | ||
await waitOnExecutionContext(ctx); | ||
expect(response.status).toBe(404); | ||
}); | ||
|
||
it("can still access assets via binding", async () => { | ||
const request = new IncomingRequest("http://example.com/binding"); | ||
const ctx = createExecutionContext(); | ||
const response = await worker.fetch(request, env, ctx); | ||
await waitOnExecutionContext(ctx); | ||
expect(await response.text()).toContain("binding.html"); | ||
}); | ||
}); | ||
|
||
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!"`); | ||
}); | ||
it("does get assets directly is using SELF", async () => { | ||
const response = await SELF.fetch("http://example.com/"); | ||
expect(await response.text()).toContain("Asset index.html"); | ||
}); | ||
it("can also get assets via binding", async () => { | ||
const response = await SELF.fetch("http://example.com/binding"); | ||
expect(await response.text()).toContain("binding.html"); | ||
}); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
fixtures/vitest-pool-workers-examples/workers-assets/test/env.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module "cloudflare:test" { | ||
interface ProvidedEnv extends Env {} | ||
} |
4 changes: 4 additions & 0 deletions
4
fixtures/vitest-pool-workers-examples/workers-assets/test/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
4 changes: 4 additions & 0 deletions
4
fixtures/vitest-pool-workers-examples/workers-assets/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../tsconfig.node.json", | ||
"include": ["./*.ts"] | ||
} |
11 changes: 11 additions & 0 deletions
11
fixtures/vitest-pool-workers-examples/workers-assets/vitest.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
}, | ||
}, | ||
}, | ||
}); |
11 changes: 11 additions & 0 deletions
11
fixtures/vitest-pool-workers-examples/workers-assets/wrangler.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
main = "src/index.ts" | ||
compatibility_date = "2024-09-19" | ||
compatibility_flags = ["nodejs_compat"] | ||
|
||
[assets] | ||
directory = "./public" | ||
binding = "ASSETS" | ||
html_handling = "auto-trailing-slash" | ||
not_found_handling = "none" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.