From 4b568776acf98bea67a99fd784ab357347631968 Mon Sep 17 00:00:00 2001 From: Carmen Popoviciu Date: Wed, 31 Jul 2024 18:47:27 +0200 Subject: [PATCH] feat(fixtures): add `workers-with-assets` fixture --- fixtures/workers-with-assets/README.md | 23 ++++++++++++++++ fixtures/workers-with-assets/package.json | 20 ++++++++++++++ .../workers-with-assets/public/index.html | 1 + .../workers-with-assets/tests/index.test.ts | 26 +++++++++++++++++++ .../workers-with-assets/tests/tsconfig.json | 7 +++++ fixtures/workers-with-assets/tsconfig.json | 13 ++++++++++ fixtures/workers-with-assets/wrangler.toml | 5 ++++ pnpm-lock.yaml | 15 +++++++++++ 8 files changed, 110 insertions(+) create mode 100644 fixtures/workers-with-assets/README.md create mode 100644 fixtures/workers-with-assets/package.json create mode 100644 fixtures/workers-with-assets/public/index.html create mode 100644 fixtures/workers-with-assets/tests/index.test.ts create mode 100644 fixtures/workers-with-assets/tests/tsconfig.json create mode 100644 fixtures/workers-with-assets/tsconfig.json create mode 100644 fixtures/workers-with-assets/wrangler.toml diff --git a/fixtures/workers-with-assets/README.md b/fixtures/workers-with-assets/README.md new file mode 100644 index 000000000000..0c5beb248c02 --- /dev/null +++ b/fixtures/workers-with-assets/README.md @@ -0,0 +1,23 @@ +# workers-with-assets + +`workers-with-assets` is a test fixture that showcases Workers with Assets. This particular fixture sets up an assets-only Workers project. + +## dev + +To start a dev session you can run + +``` +wrangler dev +``` + +or + +``` +wrangler dev --name=assets-worker --experimental-assets=public +``` + +## Run tests + +``` +npm run test +``` diff --git a/fixtures/workers-with-assets/package.json b/fixtures/workers-with-assets/package.json new file mode 100644 index 000000000000..771021f2e01c --- /dev/null +++ b/fixtures/workers-with-assets/package.json @@ -0,0 +1,20 @@ +{ + "name": "assets-worker", + "private": true, + "scripts": { + "check:type": "tsc", + "dev": "npx wrangler dev", + "test:ci": "vitest run", + "test:watch": "vitest", + "type:tests": "tsc -p ./tests/tsconfig.json" + }, + "devDependencies": { + "@cloudflare/workers-tsconfig": "workspace:*", + "@cloudflare/workers-types": "^4.20240725.0", + "undici": "^5.28.4", + "wrangler": "workspace:*" + }, + "volta": { + "extends": "../../package.json" + } +} diff --git a/fixtures/workers-with-assets/public/index.html b/fixtures/workers-with-assets/public/index.html new file mode 100644 index 000000000000..de28c4adee5c --- /dev/null +++ b/fixtures/workers-with-assets/public/index.html @@ -0,0 +1 @@ +

Hello Workers + Assets World!

diff --git a/fixtures/workers-with-assets/tests/index.test.ts b/fixtures/workers-with-assets/tests/index.test.ts new file mode 100644 index 000000000000..d128591f048c --- /dev/null +++ b/fixtures/workers-with-assets/tests/index.test.ts @@ -0,0 +1,26 @@ +import { resolve } from "node:path"; +import { fetch } from "undici"; +import { afterAll, beforeAll, describe, it } from "vitest"; +import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived"; + +describe("[Workers + Assets] `wrangler dev`", () => { + let ip: string, port: number, stop: (() => Promise) | undefined; + + beforeAll(async () => { + ({ ip, port, stop } = await runWranglerDev( + resolve(__dirname, ".."), + ["--port=0", "--inspector-port=0"] + )); + }); + + afterAll(async () => { + await stop?.(); + }); + + it("renders ", async ({ expect }) => { + const response = await fetch(`http://${ip}:${port}/`); + const text = await response.text(); + expect(response.status).toBe(200); + expect(text).toContain(`Hello from Asset Server Worker 🚀`); + }); +}); diff --git a/fixtures/workers-with-assets/tests/tsconfig.json b/fixtures/workers-with-assets/tests/tsconfig.json new file mode 100644 index 000000000000..d2ce7f144694 --- /dev/null +++ b/fixtures/workers-with-assets/tests/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "@cloudflare/workers-tsconfig/tsconfig.json", + "compilerOptions": { + "types": ["node"] + }, + "include": ["**/*.ts", "../../../node-types.d.ts"] +} diff --git a/fixtures/workers-with-assets/tsconfig.json b/fixtures/workers-with-assets/tsconfig.json new file mode 100644 index 000000000000..960979311daf --- /dev/null +++ b/fixtures/workers-with-assets/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "ES2020", + "esModuleInterop": true, + "module": "CommonJS", + "lib": ["ES2020"], + "types": ["node"], + "moduleResolution": "node", + "noEmit": true, + "skipLibCheck": true + }, + "include": ["tests", "../../node-types.d.ts"] +} \ No newline at end of file diff --git a/fixtures/workers-with-assets/wrangler.toml b/fixtures/workers-with-assets/wrangler.toml new file mode 100644 index 000000000000..d780ef6d5f09 --- /dev/null +++ b/fixtures/workers-with-assets/wrangler.toml @@ -0,0 +1,5 @@ +name = "assets-worker" +compatibility_date = "2024-01-01" + +[experimental_assets] +directory = "./public" \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bdf735428d7f..58bca6234ab9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -668,6 +668,21 @@ importers: fixtures/workers-chat-demo: {} + fixtures/workers-with-assets: + devDependencies: + '@cloudflare/workers-tsconfig': + specifier: workspace:* + version: link:../../packages/workers-tsconfig + '@cloudflare/workers-types': + specifier: ^4.20240725.0 + version: 4.20240725.0 + undici: + specifier: ^5.28.4 + version: 5.28.4 + wrangler: + specifier: workspace:* + version: link:../../packages/wrangler + packages/cli: devDependencies: '@clack/core':