-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Vitest examples to hello world templates in c3 (#5189)
* add vitest examples to hello world templates in c3 * Apply suggestions from code review Co-authored-by: MrBBot <[email protected]> * add changeset * update tests with comments * Apply suggestions from code review Co-authored-by: MrBBot <[email protected]> * Update packages/create-cloudflare/templates/hello-world/js/vitest.config.js * Update packages/create-cloudflare/templates/hello-world/ts/vitest.config.ts --------- Co-authored-by: MrBBot <[email protected]>
- Loading branch information
Showing
10 changed files
with
92 additions
and
3 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,5 @@ | ||
--- | ||
"create-cloudflare": patch | ||
--- | ||
|
||
test: add vitest-pool-workers package and example tests to `hello-world` templates |
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
20 changes: 20 additions & 0 deletions
20
packages/create-cloudflare/templates/hello-world/js/test/index.spec.js
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,20 @@ | ||
import { env, createExecutionContext, waitOnExecutionContext, SELF } from "cloudflare:test"; | ||
import { describe, it, expect } from "vitest"; | ||
import worker from "../src"; | ||
|
||
describe("Hello World worker", () => { | ||
it("responds with Hello World! (unit style)", async () => { | ||
const request = new Request("http://example.com"); | ||
// 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("responds with Hello World! (integration style)", async () => { | ||
const response = await SELF.fetch(request, env, ctx); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/create-cloudflare/templates/hello-world/js/vitest.config.js
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 { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; | ||
|
||
export default defineWorkersConfig({ | ||
test: { | ||
poolOptions: { | ||
workers: { | ||
wrangler: { configPath: "./wrangler.toml" }, | ||
}, | ||
}, | ||
}, | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/create-cloudflare/templates/hello-world/js/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
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
25 changes: 25 additions & 0 deletions
25
packages/create-cloudflare/templates/hello-world/ts/test/index.spec.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,25 @@ | ||
// test/index.spec.ts | ||
import { env, createExecutionContext, waitOnExecutionContext, SELF } from "cloudflare:test"; | ||
import { describe, it, expect } from "vitest"; | ||
import worker from "../src/index"; | ||
|
||
// For now, you'll need to do something like this to get a correctly-typed | ||
// `Request` to pass to `worker.fetch()`. | ||
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>; | ||
|
||
describe("Hello World worker", () => { | ||
it("responds with Hello World! (unit style)", async () => { | ||
const request = new IncomingRequest("http://example.com"); | ||
// 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("responds with Hello World! (integration style)", async () => { | ||
const response = await SELF.fetch("https://example.com"); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/create-cloudflare/templates/hello-world/ts/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,10 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"types": [ | ||
"@cloudflare/workers-types/experimental", | ||
"@cloudflare/vitest-pool-workers" | ||
] | ||
}, | ||
"include": ["./**/*.ts", "../src/env.d.ts"] | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/create-cloudflare/templates/hello-world/ts/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 { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; | ||
|
||
export default defineWorkersConfig({ | ||
test: { | ||
poolOptions: { | ||
workers: { | ||
wrangler: { configPath: "./wrangler.toml" }, | ||
}, | ||
}, | ||
}, | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/create-cloudflare/templates/hello-world/ts/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