Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ export default defineConfig({
});
```

If your project uses a [`cloudflare.config.ts`](/cf/projects/cloudflare-config/) file, set the `config` option instead of `wrangler`:

```ts
import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
plugins: [
cloudflareTest({
config: true,
}),
],
});
```

:::caution

Custom Vitest `environment`s or `runner`s are not supported when using the Workers Vitest integration.
Expand Down Expand Up @@ -118,10 +133,22 @@ export default defineConfig({

Options passed directly to `cloudflareTest()`.

- `config`: `boolean | string` optional
- Load `main`, [compatibility settings](/workers/configuration/compatibility-dates/), [bindings](/workers/runtime-apis/bindings/), triggers, and exports from a [`cloudflare.config.ts`](/cf/projects/cloudflare-config/) file. Pass `true` to load `cloudflare.config.ts` from the project root, or pass a path to the file relative to the project root. You cannot use `config` together with `wrangler` or `miniflare`.

- Function-form configuration receives the Vitest mode as `ctx.mode`. The mode defaults to `test`. To use a different mode, run `vitest --mode <MODE>`. Refer to [Select a mode](/cf/projects/cloudflare-config/#select-a-mode) for more information.

- Secret bindings, declared with `bindings.secret()`, are read from files in the Vite [`envDir`](https://vite.dev/config/shared-options#envdir), which is the project root by default. If `.dev.vars.<MODE>` or `.dev.vars` exists, the integration reads secrets from that file only. Otherwise, it reads `.env`, `.env.local`, `.env.<MODE>`, and `.env.<MODE>.local`, and environment variables take precedence over these files. These files do not override other bindings.

- To set a local connection string for a [Hyperdrive](/hyperdrive/) binding, set `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>` in a `.env` file or in the environment.

- [Remote bindings](/workers/local-development/#remote-bindings) authenticate with `cf` credentials. Sign in with [`cf auth login`](/cf/get-started/#sign-in), or set `CLOUDFLARE_API_TOKEN` in the environment. Apart from Hyperdrive connection strings, the integration reads `CLOUDFLARE_*` variables from the environment only, not from `.env` files.

- `main`: string optional
- Entry point to Worker run in the same isolate/context as tests. This option is required to use Durable Objects without an explicit `scriptName` if classes are defined in the same Worker. This file goes through Vite transforms and can be TypeScript. Note that `import module from "<path-to-main>"` inside tests gives exactly the same `module` instance as is used internally for `exports` and Durable Object bindings. If `wrangler.configPath` is defined and this option is not, it will be read from the `main` field in that configuration file.
- Entry point to Worker run in the same isolate/context as tests. This option is required to use Durable Objects without an explicit `scriptName` if classes are defined in the same Worker. This file goes through Vite transforms and can be TypeScript. Note that `import module from "<path-to-main>"` inside tests gives exactly the same `module` instance as is used internally for `exports` and Durable Object bindings. If `wrangler.configPath` is defined and this option is not, it will be read from the `main` field in that configuration file. If `config` is set and this option is not, the integration uses the `entrypoint` from `cloudflare.config.ts`.

- `miniflare`: `SourcelessWorkerOptions & { workers?: WorkerOptions\[]; }` optional
- You cannot use `miniflare` together with `config`.
- Use this to provide configuration information that is typically stored within the [Wrangler configuration file](/workers/wrangler/configuration/), such as [bindings](/workers/runtime-apis/bindings/), [compatibility dates](/workers/configuration/compatibility-dates/), and [compatibility flags](/workers/configuration/compatibility-flags/). The `WorkerOptions` interface is defined [here](https://github.com/cloudflare/workers-sdk/tree/main/packages/miniflare#interface-workeroptions). Use the `main` option above to configure the entry point, instead of the Miniflare `script`, `scriptPath`, or `modules` options.
- If no `compatibility_date` is provided, then the test will use the latest locally available date.

Expand All @@ -134,7 +161,7 @@ Options passed directly to `cloudflareTest()`.
- Are not affected by global mocks defined in your tests.

- `wrangler`: `{ configPath?: string; environment?: string; }` optional
- Path to [Wrangler configuration file](/workers/wrangler/configuration/) to load `main`, [compatibility settings](/workers/configuration/compatibility-dates/) and [bindings](/workers/runtime-apis/bindings/) from. These options will be merged with the `miniflare` option above, with `miniflare` values taking precedence. For example, if your Wrangler configuration defined a [service binding](/workers/runtime-apis/bindings/service-bindings/) named `SERVICE` to a Worker named `service`, but you included `serviceBindings: { SERVICE(request) { return new Response("body"); } }` in the `miniflare` option, all requests to `SERVICE` in tests would return `body`. Note `configPath` accepts both `.toml` and `.json` files.
- Path to [Wrangler configuration file](/workers/wrangler/configuration/) to load `main`, [compatibility settings](/workers/configuration/compatibility-dates/) and [bindings](/workers/runtime-apis/bindings/) from. These options will be merged with the `miniflare` option above, with `miniflare` values taking precedence. For example, if your Wrangler configuration defined a [service binding](/workers/runtime-apis/bindings/service-bindings/) named `SERVICE` to a Worker named `service`, but you included `serviceBindings: { SERVICE(request) { return new Response("body"); } }` in the `miniflare` option, all requests to `SERVICE` in tests would return `body`. Note `configPath` accepts both `.toml` and `.json` files. You cannot use `wrangler` together with `config`.

- The environment option can be used to specify the [Wrangler environment](/workers/wrangler/environments/) to pick up bindings and variables from.

Expand Down
1 change: 1 addition & 0 deletions src/content/partials/workers/cloudflare-config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ configuration:
| Command | Default mode |
| ---------------------------------------------------------------- | ------------- |
| `cf dev` with the Cloudflare Vite plugin | `development` |
| `vitest` with the Workers Vitest integration | `test` |
| `cf build`, `cf deploy`, and other commands that build with Vite | `production` |
| Commands that build with Wrangler | `undefined` |
| API commands, such as `cf d1 list` | `undefined` |
Expand Down
Loading