Skip to content

Commit

Permalink
add assets only vitest fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
emily-shen committed Sep 27, 2024
1 parent 14b9044 commit b2a1791
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 73 deletions.
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.
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,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);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.workerd-test.json",
"include": ["./**/*.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,19 @@
import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config";

export default defineWorkersProject({
test: {
poolOptions: {
workers: {
wrangler: { configPath: "./wrangler.toml" },
miniflare: {
assets: {
directory: "./public",
assetConfig: {
html_handling: "none",
},
},
},
},
},
},
});
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"
78 changes: 5 additions & 73 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b2a1791

Please sign in to comment.