You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/workers/testing/index.mdx
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,16 +7,16 @@ sidebar:
7
7
8
8
import { Render, LinkButton } from"~/components";
9
9
10
-
The Workers platform has a variety of ways to test your applications, depending on your requirements. We recommend using the [Vitest integration](/workers/testing/vitest-integration), which allows for unit testing individual functions within your Worker.
10
+
The Workers platform has a variety of ways to test your applications, depending on your requirements. We recommend using the [Vitest integration](/workers/testing/vitest-integration), which allows you to run tests to _inside_ the Workers runtime, and unit test individual functions within your Worker.
However, if you don't use Vitest, both [Miniflare's API](/workers/testing/miniflare/writing-tests) and the [`unstable_startWorker()`](/workers/wrangler/api/#unstable_startworker) API provide options for testing your Worker in any testing framework.
17
-
18
16
## Testing comparison matrix
19
17
18
+
However, if you don't use Vitest, both [Miniflare's API](/workers/testing/miniflare/writing-tests) and the [`unstable_startWorker()`](/workers/wrangler/api/#unstable_startworker) API provide options for testing your Worker in any testing framework.
Copy file name to clipboardExpand all lines: src/content/docs/workers/testing/vitest-integration/get-started/index.mdx
-20Lines changed: 0 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,24 +11,4 @@ description: Install and set up the Workers Vitest integration.
11
11
12
12
import { DirectoryListing } from"~/components";
13
13
14
-
For most users, Cloudflare recommends using the Workers Vitest integration for testing Workers and [Pages Functions](/pages/functions/) projects. [Vitest](https://vitest.dev/) is a popular JavaScript testing framework featuring a very fast watch mode, Jest compatibility, and out-of-the-box support for TypeScript. In this integration, Cloudflare provides a custom pool that allows your Vitest tests to run _inside_ the Workers runtime.
15
-
16
-
The Workers Vitest integration:
17
-
18
-
- Supports both **unit tests** and **integration tests**.
19
-
- Provides direct access to Workers runtime APIs and bindings.
20
-
- Implements isolated per-test storage.
21
-
- Runs tests fully-locally using [Miniflare](https://miniflare.dev/).
22
-
- Leverages Vitest's hot-module reloading for near instant reruns.
23
-
- Provides a declarative interface for mocking outbound requests.
24
-
- Supports projects with multiple Workers.
25
-
26
-
Get started with one of the available guides:
27
-
28
14
<DirectoryListing />
29
-
30
-
:::caution
31
-
32
-
The Workers Vitest integration does not support testing Workers using the service worker format. [Migrate to the ES modules format](/workers/reference/migrate-to-module-workers/) to use the Workers Vitest integration.
2.You can override configuration using the `miniflare` key.
59
+
You can also override or define additional configuration using the `miniflare` key. This takes precedence over values set in via your Wrangler config.
63
60
64
-
For example, this configuration would add a KV namespace `TEST_NAMESPACE` that was only accessed and modified in tests.
61
+
For example, this configuration would add a KV namespace `TEST_NAMESPACE` that was only accessed and modified in tests.
65
62
66
63
```js null {6-8}
67
64
exportdefaultdefineWorkersConfig({
@@ -84,9 +81,12 @@ For a full list of available configuration options, refer to [Configuration](/wo
84
81
85
82
## Define types
86
83
87
-
First make sure you have run [`wrangler types`](/workers/wrangler/commands/), which generates `Env` types based on your Worker's bindings and [runtime types](/workers/languages/typescript/).
84
+
If you are not using Typescript, you can skip this section.
85
+
86
+
First make sure you have run [`wrangler types`](/workers/wrangler/commands/), which generates [types for the Cloudflare Workers runtime](/workers/languages/typescript/) and an `Env` type based on your Worker's bindings.
88
87
89
-
If you are using TypeScript, you will need to define types for Cloudflare Workers and `cloudflare:test` to make sure they are detected appropriately. Add a `tsconfig.json` in your tests folder and add `"@cloudflare/vitest-pool-workers"` to your types array.
88
+
Then add a `tsconfig.json` in your tests folder and add `"@cloudflare/vitest-pool-workers"` to your types array to define types for `cloudflare:test`.
89
+
You should also add the output of `wrangler types` to the `include` array so that the types for the Cloudflare Workers runtime are available.
90
90
91
91
<Detailsheader="Example test/tsconfig.json">
92
92
```jsonc title="test/tsconfig.json"
@@ -106,7 +106,7 @@ If you are using TypeScript, you will need to define types for Cloudflare Worker
106
106
```
107
107
</Details>
108
108
109
-
You also need to define the type of the `env` object that is passed to your tests. Create an `env.d.ts` file in your tests folder, with the following content:
109
+
You also need to define the type of the `env` object that is provided to your tests. Create an `env.d.ts` file in your tests folder, and declare the `ProvidedEnv` interface by extending the `Env` interface that is generated by `wrangler types`.
If your test bindings differ from the bindings in your Wrangler config, you should type them here in `ProvidedEnv`.
119
119
120
-
## Write a unit test
120
+
## Writing tests
121
121
122
-
Given an example Worker like this:
122
+
We will use this simple Worker as an example. It returns a 404 response for the `/404` path and `"Hello World!"` for all other paths.
123
123
124
124
<TypeScriptExamplefilename="src/index.ts">
125
125
```ts
@@ -132,9 +132,12 @@ export default {
132
132
},
133
133
} satisfiesExportedHandler<Env>;
134
134
```
135
+
135
136
</TypeScriptExample>
136
137
137
-
You might want to add a test like this to check that the Worker response correctly to a request at `/404`.
138
+
### Unit tests
139
+
140
+
By importing the Worker we can write a unit test for its `fetch` handler.
138
141
139
142
<TypeScriptExamplefilename="test/unit.spec.ts">
140
143
```ts
@@ -168,7 +171,7 @@ You might want to add a test like this to check that the Worker response correct
168
171
169
172
</TypeScriptExample>
170
173
171
-
##Write an integration test
174
+
### Integration tests
172
175
173
176
You can use the SELF fetcher provided by the `cloudflare:test` to write an integration test. This is a service binding to the default export defined in the main Worker. The main Worker runs in the same isolate/context as tests so any global mocks will apply to it too.
0 commit comments