Skip to content

Commit a881733

Browse files
Introduce vitest4 to vitest-pool-workers (#28908)
* Add changelog for vitest4 upgrade to pool-workers * Update docs for vitest 4.1 release * Apply suggestions from code review Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com> * Update with further information on Vitest 4 benefits * chore: remove changelog from docs branch (moved to separate branch) * fix: prettier on save formatting * fix: address review comment on type generation * fix: correct note in test guide --------- Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com>
1 parent 587de6e commit a881733

12 files changed

Lines changed: 361 additions & 545 deletions

File tree

src/content/docs/durable-objects/best-practices/rules-of-durable-objects.mdx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,8 +1498,8 @@ Use `@cloudflare/vitest-pool-workers` for testing Durable Objects. The integrati
14981498

14991499
<TypeScriptExample filename="test/chat-room.test.ts">
15001500
```ts
1501+
import { env } from "cloudflare:workers";
15011502
import {
1502-
env,
15031503
runInDurableObject,
15041504
runDurableObjectAlarm,
15051505
} from "cloudflare:test";
@@ -1543,16 +1543,15 @@ describe("ChatRoom", () => {
15431543
Configure Vitest in your `vitest.config.ts`:
15441544

15451545
```ts
1546-
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
1547-
1548-
export default defineWorkersConfig({
1549-
test: {
1550-
poolOptions: {
1551-
workers: {
1552-
wrangler: { configPath: "./wrangler.jsonc" },
1553-
},
1554-
},
1555-
},
1546+
import { cloudflareTest } from "@cloudflare/vitest-pool-workers";
1547+
import { defineConfig } from "vitest/config";
1548+
1549+
export default defineConfig({
1550+
plugins: [
1551+
cloudflareTest({
1552+
wrangler: { configPath: "./wrangler.jsonc" },
1553+
}),
1554+
],
15561555
});
15571556
```
15581557

src/content/docs/durable-objects/examples/testing-with-durable-objects.mdx

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ Install Vitest and the Workers Vitest integration as dev dependencies:
1919
<Tabs>
2020
<TabItem label="npm">
2121
```sh
22-
npm i -D vitest@~3.2.0 @cloudflare/vitest-pool-workers
22+
npm i -D vitest@^4.1.0 @cloudflare/vitest-pool-workers
2323
```
2424
</TabItem>
2525
<TabItem label="pnpm">
2626
```sh
27-
pnpm add -D vitest@~3.2.0 @cloudflare/vitest-pool-workers
27+
pnpm add -D vitest@^4.1.0 @cloudflare/vitest-pool-workers
2828
```
2929
</TabItem>
3030
<TabItem label="yarn">
3131
```sh
32-
yarn add -D vitest@~3.2.0 @cloudflare/vitest-pool-workers
32+
yarn add -D vitest@^4.1.0 @cloudflare/vitest-pool-workers
3333
```
3434
</TabItem>
3535
</Tabs>
@@ -106,19 +106,18 @@ export default {
106106

107107
## Configure Vitest
108108

109-
Create a `vitest.config.ts` file that uses `defineWorkersConfig`:
109+
Create a `vitest.config.ts` file that uses the `cloudflareTest()` plugin:
110110

111111
```ts title="vitest.config.ts"
112-
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
113-
114-
export default defineWorkersConfig({
115-
test: {
116-
poolOptions: {
117-
workers: {
118-
wrangler: { configPath: "./wrangler.jsonc" },
119-
},
120-
},
121-
},
112+
import { cloudflareTest } from "@cloudflare/vitest-pool-workers";
113+
import { defineConfig } from "vitest/config";
114+
115+
export default defineConfig({
116+
plugins: [
117+
cloudflareTest({
118+
wrangler: { configPath: "./wrangler.jsonc" },
119+
}),
120+
],
122121
});
123122
```
124123

@@ -160,7 +159,7 @@ Create a `test/tsconfig.json` to configure TypeScript for your tests:
160159
Create an `env.d.ts` file to type the test environment:
161160

162161
```ts title="test/env.d.ts"
163-
declare module "cloudflare:test" {
162+
declare module "cloudflare:workers" {
164163
interface ProvidedEnv extends Env {}
165164
}
166165
```
@@ -169,11 +168,11 @@ declare module "cloudflare:test" {
169168

170169
### Unit tests with direct Durable Object access
171170

172-
You can get a stub to a Durable Object directly from the `env` object provided by `cloudflare:test`:
171+
You can get a stub to a Durable Object directly from the `env` object provided by `cloudflare:workers`:
173172

174173
<TypeScriptExample filename="test/counter.test.ts">
175174
```ts
176-
import { env } from "cloudflare:test";
175+
import { env } from "cloudflare:workers";
177176
import { describe, it, expect, beforeEach } from "vitest";
178177

179178
describe("Counter Durable Object", () => {
@@ -237,18 +236,18 @@ describe("Counter Durable Object", () => {
237236
```
238237
</TypeScriptExample>
239238

240-
### Integration tests with SELF
239+
### Integration tests with `exports`
241240

242-
Use the `SELF` fetcher to test your Worker's HTTP handler, which routes requests to Durable Objects:
241+
Use `exports.default.fetch()` to test your Worker's HTTP handler, which routes requests to Durable Objects:
243242

244243
<TypeScriptExample filename="test/integration.test.ts">
245244
```ts
246-
import { SELF } from "cloudflare:test";
245+
import { exports } from "cloudflare:workers";
247246
import { describe, it, expect } from "vitest";
248247

249248
describe("Counter Worker integration", () => {
250249
it("should increment via HTTP POST", async () => {
251-
const response = await SELF.fetch("http://example.com?id=http-test", {
250+
const response = await exports.default.fetch("http://example.com?id=http-test", {
252251
method: "POST",
253252
});
254253

@@ -259,22 +258,22 @@ describe("Counter Worker integration", () => {
259258

260259
it("should get count via HTTP GET", async () => {
261260
// First increment the counter
262-
await SELF.fetch("http://example.com?id=get-test", { method: "POST" });
263-
await SELF.fetch("http://example.com?id=get-test", { method: "POST" });
261+
await exports.default.fetch("http://example.com?id=get-test", { method: "POST" });
262+
await exports.default.fetch("http://example.com?id=get-test", { method: "POST" });
264263

265264
// Then get the count
266-
const response = await SELF.fetch("http://example.com?id=get-test");
265+
const response = await exports.default.fetch("http://example.com?id=get-test");
267266
const data = await response.json<{ count: number }>();
268267
expect(data.count).toBe(2);
269268
});
270269

271270
it("should use different counters for different IDs", async () => {
272-
await SELF.fetch("http://example.com?id=counter-a", { method: "POST" });
273-
await SELF.fetch("http://example.com?id=counter-a", { method: "POST" });
274-
await SELF.fetch("http://example.com?id=counter-b", { method: "POST" });
271+
await exports.default.fetch("http://example.com?id=counter-a", { method: "POST" });
272+
await exports.default.fetch("http://example.com?id=counter-a", { method: "POST" });
273+
await exports.default.fetch("http://example.com?id=counter-b", { method: "POST" });
275274

276-
const responseA = await SELF.fetch("http://example.com?id=counter-a");
277-
const responseB = await SELF.fetch("http://example.com?id=counter-b");
275+
const responseA = await exports.default.fetch("http://example.com?id=counter-a");
276+
const responseB = await exports.default.fetch("http://example.com?id=counter-b");
278277

279278
const dataA = await responseA.json<{ count: number }>();
280279
const dataB = await responseB.json<{ count: number }>();
@@ -292,8 +291,8 @@ Use `runInDurableObject()` to access instance properties and storage directly. T
292291

293292
<TypeScriptExample filename="test/direct-access.test.ts">
294293
```ts
294+
import { env } from "cloudflare:workers";
295295
import {
296-
env,
297296
runInDurableObject,
298297
listDurableObjectIds,
299298
} from "cloudflare:test";
@@ -349,7 +348,8 @@ Each test automatically gets isolated storage. Durable Objects created in one te
349348

350349
<TypeScriptExample filename="test/isolation.test.ts">
351350
```ts
352-
import { env, listDurableObjectIds } from "cloudflare:test";
351+
import { env } from "cloudflare:workers";
352+
import { listDurableObjectIds } from "cloudflare:test";
353353
import { describe, it, expect } from "vitest";
354354

355355
describe("Test isolation", () => {
@@ -382,7 +382,8 @@ SQLite-backed Durable Objects work seamlessly in tests. The SQL API is available
382382

383383
<TypeScriptExample filename="test/sqlite.test.ts">
384384
```ts
385-
import { env, runInDurableObject } from "cloudflare:test";
385+
import { env } from "cloudflare:workers";
386+
import { runInDurableObject } from "cloudflare:test";
386387
import { describe, it, expect } from "vitest";
387388

388389
describe("SQLite in Durable Objects", () => {
@@ -421,8 +422,8 @@ Use `runDurableObjectAlarm()` to immediately trigger a scheduled alarm without w
421422

422423
<TypeScriptExample filename="test/alarm.test.ts">
423424
```ts
425+
import { env } from "cloudflare:workers";
424426
import {
425-
env,
426427
runInDurableObject,
427428
runDurableObjectAlarm,
428429
} from "cloudflare:test";

src/content/docs/workers/best-practices/workers-best-practices.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ One known pitfall: the Vitest pool automatically injects `nodejs_compat`, so tes
918918

919919
```ts
920920
import { describe, it, expect } from "vitest";
921-
import { env } from "cloudflare:test";
921+
import { env } from "cloudflare:workers";
922922

923923
describe("KV operations", () => {
924924
it("should store and retrieve a value", async () => {

0 commit comments

Comments
 (0)