Skip to content

Commit 96c5598

Browse files
committed
docs(vitest): document buildPagesASSETSBinding()
1 parent 84745ee commit 96c5598

File tree

2 files changed

+103
-10
lines changed

2 files changed

+103
-10
lines changed

src/content/docs/workers/testing/vitest-integration/configuration.mdx

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,117 @@ Custom Vitest `environment`s or `runner`s are not supported when using the Worke
4646

4747
:::
4848

49-
## Functions
49+
## APIs
5050

51-
The following functions are exported from the `@cloudflare/vitest-pool-workers/config` module.
51+
The following APIs are exported from the `@cloudflare/vitest-pool-workers/config` module.
5252

53+
### `defineWorkersConfig(options)`
5354

55+
Ensures Vitest is configured to use the Workers integration with the correct module resolution settings, and provides type checking for [WorkersPoolOptions](#workerspooloptions). This should be used in place of the [`defineConfig()`](https://vitest.dev/config/file.html) function from Vitest. The `defineWorkersConfig()` function also accepts a `Promise` of `options`, or an optionally-`async` function returning `options`.
5456

55-
* `defineWorkersConfig(options:UserConfig & { test?: { poolOptions?: { workers?: WorkersPoolOptions | ((ctx: WorkerPoolOptionsContext) => Awaitable\<WorkersPoolOptions>) } } })`
57+
```ts
58+
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
5659

57-
* Ensures Vitest is configured to use the Workers integration with the correct module resolution settings, and provides type checking for `WorkersPoolOptions`. This should be used in place of the [`defineConfig()`](https://vitest.dev/config/file.html) function from Vitest. The `defineWorkersConfig()` function also accepts a `Promise` of `options`, or an optionally-`async` function returning `options`.
60+
export default defineWorkersConfig({
61+
test: {
62+
poolOptions: {
63+
workers: {
64+
// ...
65+
},
66+
},
67+
},
68+
});
69+
```
5870

59-
* `defineWorkersProject(options:UserWorkspaceConfig & { test?: { poolOptions?: { workers?: WorkersPoolOptions | ((ctx: WorkerPoolOptionsContext) => Awaitable\<WorkersPoolOptions>) } } })`
71+
### `defineWorkersProject(options)`
6072

61-
* Ensures Vitest is configured to use the Workers integration with the correct module resolution settings, and provides type checking for `WorkersPoolOptions`. This should be used in place of the [`defineProject()`](https://vitest.dev/guide/workspace) function from Vitest. The `defineWorkersProject()` function also accepts a `Promise` of `options`, or an optionally-`async` function returning `options`.
73+
Ensures Vitest is configured to use the Workers integration with the correct module resolution settings, and provides type checking for [WorkersPoolOptions](#workerspooloptions) similar to `defineWorkersConfig`, except this should be used in place of the [`defineProject()`](https://vitest.dev/guide/workspace) function from Vitest. The `defineWorkersProject()` function also accepts a `Promise` of `options`, or an optionally-`async` function returning `options`.
6274

63-
* `readD1Migrations(migrationsPath:string)`: `D1Migration[]`
75+
```ts
76+
import { defineWorkspace, defineProject } from "vitest/config";
77+
import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config";
78+
79+
const workspace = defineWorkspace([
80+
defineWorkersProject({
81+
test: {
82+
name: "Workers",
83+
include: ["**/*.worker.test.ts"],
84+
poolOptions: {
85+
workers: {
86+
// ...
87+
},
88+
},
89+
},
90+
}),
91+
92+
// ...
93+
]);
94+
95+
export default workspace;
96+
```
97+
98+
### `buildPagesASSETSBinding(assetsPath)`
99+
100+
Creates a Pages ASSETS binding that serves files insides the `assetsPath`. This is required if you uses `createPagesEventContext()` to test your Pages function. Refer to the [Pages recipe](/workers/testing/vitest-integration/recipes) for an example on testing Pages function.
64101

65-
* Reads all [D1 migrations](/d1/reference/migrations/) stored at `migrationsPath` and returns them ordered by migration number. Each migration will have its contents split into an array of individual SQL queries. Call the [`applyD1Migrations()`](/workers/testing/vitest-integration/test-apis/#d1) function inside a test or [setup file](https://vitest.dev/config/#setupfiles) to apply migrations. Refer to the [D1 recipe](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/d1) for an example project using migrations.
102+
```ts
103+
import path from "node:path";
104+
import {
105+
buildPagesASSETSBinding,
106+
defineWorkersProject,
107+
} from "@cloudflare/vitest-pool-workers/config";
108+
109+
export default defineWorkersProject(async () => {
110+
const assetsPath = path.join(__dirname, "public");
111+
112+
return {
113+
test: {
114+
poolOptions: {
115+
workers: {
116+
miniflare: {
117+
serviceBindings: {
118+
ASSETS: await buildPagesASSETSBinding(assetsPath),
119+
},
120+
},
121+
},
122+
},
123+
},
124+
};
125+
});
66126

127+
```
67128

129+
### `readD1Migrations(migrationsPath)`
130+
131+
Reads all [D1 migrations](/d1/reference/migrations/) stored at `migrationsPath` and returns them ordered by migration number. Each migration will have its contents split into an array of individual SQL queries. Call the [`applyD1Migrations()`](/workers/testing/vitest-integration/test-apis/#d1) function inside a test or [setup file](https://vitest.dev/config/#setupfiles) to apply migrations. Refer to the [D1 recipe](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/d1) for an example project using migrations.
132+
133+
```ts
134+
import path from "node:path";
135+
import {
136+
defineWorkersProject,
137+
readD1Migrations,
138+
} from "@cloudflare/vitest-pool-workers/config";
139+
140+
export default defineWorkersProject(async () => {
141+
// Read all migrations in the `migrations` directory
142+
const migrationsPath = path.join(__dirname, "migrations");
143+
const migrations = await readD1Migrations(migrationsPath);
144+
145+
return {
146+
test: {
147+
setupFiles: ["./test/apply-migrations.ts"],
148+
poolOptions: {
149+
workers: {
150+
miniflare: {
151+
// Add a test-only binding for migrations, so we can apply them in a setup file
152+
bindings: { TEST_MIGRATIONS: migrations },
153+
},
154+
},
155+
},
156+
},
157+
};
158+
});
159+
```
68160

69161
## `WorkersPoolOptions`
70162

src/content/docs/workers/testing/vitest-integration/recipes.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ description: Examples that demonstrate how to write unit tests and integration
1010

1111
Recipes are examples that help demonstrate how to write unit tests and integration tests for Workers projects using the [`@cloudflare/vitest-pool-workers`](https://www.npmjs.com/package/@cloudflare/vitest-pool-workers) package.
1212

13-
- [Basic unit and integration tests using `SELF`](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/basics-unit-integration-self)
13+
- [Basic unit and integration tests for Workers using `SELF`](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/basics-unit-integration-self)
14+
- [Basic unit and integration tests for Pages function using `SELF`](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/pages-functions-unit-integration-self)
1415
- [Basic integration tests using an auxiliary Worker](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/basics-integration-auxiliary)
15-
- [Basic integration test for a Worker with static assets](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/workers-assets)
16+
- [Basic integration test for Workers with static assets](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/workers-assets)
1617
- [Isolated tests using KV, R2 and the Cache API](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/kv-r2-caches)
1718
- [Isolated tests using D1 with migrations](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/d1)
1819
- [Isolated tests using Durable Objects with direct access](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/durable-objects)

0 commit comments

Comments
 (0)