Skip to content

Commit 1aee990

Browse files
authored
Update vitest integration recipes (#13548)
1 parent 4fda685 commit 1aee990

108 files changed

Lines changed: 579 additions & 461 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/clear-turkeys-boil.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
Update warning message when attempting to access exports not defined on the main worker
6+
7+
Previously this referred to the `SELF` worker, which is now a deprecated API in the Vitest integration.
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# `@cloudflare/vitest-pool-workers` Examples
22

3-
This directory contains example projects tested with `@cloudflare/vitest-pool-workers`. It aims to provide the building blocks for you to write tests for your own Workers. Note the examples in this directory define `singleWorker: true` options. We recommend you enable this option if you have lots of small test files. Isolated storage is enabled by default meaning writes performed in each test are automatically undone when the test finishes.
3+
This directory contains example projects tested with `@cloudflare/vitest-pool-workers`. It aims to provide the building blocks for you to write tests for your own Workers.
44

5-
| Directory | Overview |
6-
| --------------------------------------------------------------------------------- | --------------------------------------------------------- |
7-
| [✅ basics-unit-integration-self](basics-unit-integration-self) | Basic unit tests and integration tests using `SELF` |
8-
| [⚠️ basics-integration-auxiliary](basics-integration-auxiliary) | Basic integration tests using an auxiliary worker[^1] |
9-
| [⚡️ pages-functions-unit-integration-self](pages-functions-unit-integration-self) | Functions unit tests and integration tests using `SELF` |
10-
| [📦 kv-r2-caches](kv-r2-caches) | Isolated tests using KV, R2 and the Cache API |
11-
| [📚 d1](d1) | Isolated tests using D1 with migrations |
12-
| [📌 durable-objects](durable-objects) | Isolated tests using Durable Objects with direct access |
13-
| [🔁 workflows](workflows) | Tests using Workflows |
14-
| [🚥 queues](queues) | Tests using Queue producers and consumers |
15-
| [🚰 pipelines](pipelines) | Tests using Pipelines |
16-
| [🚀 hyperdrive](hyperdrive) | Tests using Hyperdrive with a Vitest managed TCP server |
17-
| [🤹 request-mocking](request-mocking) | Tests using declarative/imperative outbound request mocks |
18-
| [🔌 multiple-workers](multiple-workers) | Tests using multiple auxiliary workers and request mocks |
19-
| [⚙️ web-assembly](web-assembly) | Tests importing WebAssembly modules |
20-
| [🤯 rpc](rpc) | Tests using named entrypoints, Durable Objects and RPC |
21-
| [🤷 misc](misc) | Tests for other assorted Vitest features |
5+
| Directory | Overview |
6+
| --------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
7+
| [✅ basics-unit-integration-self](basics-unit-integration-self) | Basic unit tests and integration tests using `exports.default` |
8+
| [⚠️ basics-integration-auxiliary](basics-integration-auxiliary) | Basic integration tests using an auxiliary worker[^1] |
9+
| [⚡️ pages-functions-unit-integration-self](pages-functions-unit-integration-self) | Functions unit tests and integration tests using `exports.default` |
10+
| [📦 kv-r2-caches](kv-r2-caches) | Tests using KV, R2 and the Cache API |
11+
| [📚 d1](d1) | Tests using D1 with migrations |
12+
| [📌 durable-objects](durable-objects) | Tests using Durable Objects with direct access |
13+
| [🔁 workflows](workflows) | Tests using Workflows |
14+
| [🚥 queues](queues) | Tests using Queue producers and consumers |
15+
| [🚰 pipelines](pipelines) | Tests using Pipelines |
16+
| [🚀 hyperdrive](hyperdrive) | Tests using Hyperdrive with a Vitest managed TCP server |
17+
| [🤹 request-mocking](request-mocking) | Tests using declarative (MSW) / imperative outbound request mocks |
18+
| [🔌 multiple-workers](multiple-workers) | Tests using multiple auxiliary workers and request mocks |
19+
| [⚙️ web-assembly](web-assembly) | Tests importing WebAssembly modules |
20+
| [🤯 rpc](rpc) | Tests using named entrypoints, Durable Objects and RPC |
21+
| [🧠 ai-vectorize](ai-vectorize) | Tests using Workers AI and Vectorize |
22+
| [🔄 context-exports](context-exports) | Tests using context exports |
23+
| [📥 dynamic-import](dynamic-import) | Tests using dynamic imports |
24+
| [🖼️ images](images) | Tests using the Images binding |
2225

23-
[^1]: When using `SELF` for integration tests, your worker code runs in the same context as the test runner. This means you can use global mocks to control your worker, but also means your worker uses the same subtly different module resolution behaviour provided by Vite. Usually this isn't a problem, but if you'd like to run your worker in a fresh environment that's as close to production as possible, using an auxiliary worker may be a good idea. Note this prevents global mocks from controlling your worker, and requires you to build your worker ahead-of-time. This means your tests won't re-run automatically if you change your worker's source code, but could be useful if you have a complicated build process (e.g. full-stack framework).
26+
[^1]: When using `exports.default` for integration tests, your worker code runs in the same context as the test runner. This means you can use global mocks to control your worker, but also means your worker uses the same subtly different module resolution behaviour provided by Vite. Usually this isn't a problem, but if you'd like to run your worker in a fresh environment that's as close to production as possible, using an auxiliary worker may be a good idea. Note this prevents global mocks from controlling your worker, and requires you to build your worker ahead-of-time. This means your tests won't re-run automatically if you change your worker's source code, but could be useful if you have a complicated build process (e.g. full-stack framework).

fixtures/vitest-pool-workers-examples/ai-vectorize/src/env.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
// Generated by Wrangler by running `wrangler types` (hash: 266f7400cc06616d9ccb541889857ea7)
1+
/* eslint-disable */
2+
// Generated by Wrangler by running `wrangler types --no-include-runtime ./src/env.d.ts` (hash: 59ee4c56d833f37b4bc751d34197c037)
23
declare namespace Cloudflare {
4+
interface GlobalProps {
5+
mainModule: typeof import("./index");
6+
}
37
interface Env {
48
VECTORIZE: VectorizeIndex;
59
AI: Ai;

fixtures/vitest-pool-workers-examples/ai-vectorize/test/env.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

fixtures/vitest-pool-workers-examples/ai-vectorize/test/index.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {
22
createExecutionContext,
3-
env,
4-
SELF,
53
waitOnExecutionContext,
64
} from "cloudflare:test";
5+
import { env, exports } from "cloudflare:workers";
76
import { describe, it, vi } from "vitest";
87
import worker from "../src/index";
98

@@ -75,7 +74,7 @@ describe("Tests that do not hit the AI binding", () => {
7574
});
7675

7776
it("responds with Hello World! (integration style)", async ({ expect }) => {
78-
const response = await SELF.fetch("https://example.com");
77+
const response = await exports.default.fetch("https://example.com");
7978
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
8079
});
8180
});

fixtures/vitest-pool-workers-examples/basics-integration-auxiliary/test/fetch-integration-auxiliary.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { env } from "cloudflare:test";
1+
import { env } from "cloudflare:workers";
22
import { it } from "vitest";
33

44
it("dispatches fetch event", async ({ expect }) => {

fixtures/vitest-pool-workers-examples/basics-integration-auxiliary/test/scheduled-integration-auxiliary.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { env } from "cloudflare:test";
1+
import { env } from "cloudflare:workers";
22
import { it } from "vitest";
33

44
it("dispatches scheduled event", async ({ expect }) => {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# ✅ basics-unit-integration-self
22

3-
This Worker contains basic `fetch` and `scheduled` handlers. Integration tests dispatch events using the `SELF` helper from the `cloudflare:test` module. Unit tests call handler functions directly.
3+
This Worker contains basic `fetch` and `scheduled` handlers. Integration tests dispatch events using `exports` from the `cloudflare:workers` module. Unit tests call handler functions directly.
44

55
| Test | Overview |
66
| ----------------------------------------------------------------------------- | ----------------------------------------------------- |
7-
| [fetch-integration-self.test.ts](test/fetch-integration-self.test.ts) | Basic `fetch` integration test using `SELF` |
7+
| [fetch-integration-self.test.ts](test/fetch-integration-self.test.ts) | Basic `fetch` integration test using `exports` |
88
| [fetch-unit.test.ts](test/fetch-unit.test.ts) | Basic unit test calling `worker.fetch()` directly |
9-
| [scheduled-integration-self.test.ts](test/scheduled-integration-self.test.ts) | Basic `scheduled` integration test using `SELF` |
9+
| [scheduled-integration-self.test.ts](test/scheduled-integration-self.test.ts) | Basic `scheduled` integration test using `exports` |
1010
| [scheduled-unit.test.ts](test/scheduled-unit.test.ts) | Basic unit test calling `worker.scheduled()` directly |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable */
2+
// Generated by Wrangler by running `wrangler types --no-include-runtime ./src/env.d.ts` (hash: 95117834d21499390beb126f578a90f2)
3+
declare namespace Cloudflare {
4+
interface GlobalProps {
5+
mainModule: typeof import("./index");
6+
}
7+
interface Env {}
8+
}
9+
interface Env extends Cloudflare.Env {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function greet(request: Request): string {
2+
return `👋 ${request.url}`;
3+
}

0 commit comments

Comments
 (0)