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
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. 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.
Integration tests test multiple units of your Worker together by sending HTTP requests to your Worker and asserting on the HTTP responses. As an example, consider the following Worker:
13
14
@@ -84,32 +85,30 @@ This method is less recommended than `SELF` for integration tests because of its
If you do not want to use Vitest and would like to write integration tests for a single Worker, consider using [Wrangler's `unstable_dev()` API](/workers/wrangler/api/#unstable_dev). `unstable_dev()` allows you to start an HTTP server similar to [`wrangler dev`](/workers/wrangler/commands/#dev) that you can send HTTP requests to. `unstable_dev()` will automatically load options from your Wrangler configuration file. Note that `unstable_dev()` is an experimental API subject to breaking changes.
90
+
:::caution
91
+
`unstable_startWorker()` is an experimental API subject to breaking changes.
92
+
:::
93
+
94
+
If you do not want to use Vitest and would like to write integration tests for a single Worker, consider using [Wrangler's `unstable_startWorker()` API](/workers/wrangler/api/#unstable_startworker). This API exposes the internals of Wrangler's dev server, and allows you to customise how it runs.
If you have been using `unstable_dev()` for integration testing and want to migrate to Cloudflare's Vitest integration, refer to the [Migrate from `unstable_dev` migration guide](/workers/testing/vitest-integration/get-started/migrate-from-unstable-dev/) for more information.
If you would like to write integration tests for multiple Workers, need direct access to [bindings](/workers/runtime-apis/bindings/) outside your Worker in tests, or have another advanced use case, consider using [Miniflare's API](/workers/testing/miniflare) directly. Miniflare is the foundation for the other testing tools on this page, exposing a JavaScript API for the [`workerd` runtime](https://github.com/cloudflare/workerd) and local simulators for the other Developer Platform products. Unlike `unstable_dev()`, Miniflare does not automatically load options from your Wrangler configuration file.
111
+
If you would like to write integration tests for multiple Workers, need direct access to [bindings](/workers/runtime-apis/bindings/) outside your Worker in tests, or have another advanced use case, consider using [Miniflare's API](/workers/testing/miniflare) directly. Miniflare is the foundation for the other testing tools on this page, exposing a JavaScript API for the [`workerd` runtime](https://github.com/cloudflare/workerd) and local simulators for the other Developer Platform products. Unlike `unstable_startWorker()`, Miniflare does not automatically load options from your Wrangler configuration file. Refer to the [Writing Tests](/workers/testing/miniflare/writing-tests/) page for an example of how to use Miniflare together with `node:test`.
This guide will instruct you through setting up [Miniflare](/workers/testing/miniflare) for testing your Workers. Miniflare is a low-level API that allows you to fully control how your Workers are run and tested.
@@ -96,11 +96,25 @@ You should be able to run the above test via `node --test`
96
96
The highlighted lines of the test file above demonstrate how to set up Miniflare to run a JavaScript Worker. Once Miniflare has been set up, your individual tests can send requests to the running Worker and assert against the responses. This is the main limitation of using Miniflare for testing your Worker as compared to the [Vitest integration](/workers/testing/vitest-integration/)—all access to your Worker must be through the `dispatchFetch()` Miniflare API, and you cannot unit test individual functions from your Worker.
97
97
98
98
<Detailsheader="What runtime are tests running in?">
99
-
When using the [Vitest integration](/workers/testing/vitest-integration/), your entire test suite runs in [`workerd`](https://github.com/cloudflare/workerd), which is why it's possible to unit test individual functions. By contrast, when using a different testing framework to run tests via Miniflare, only your Worker itself is running in [`workerd`](https://github.com/cloudflare/workerd)—your test files run in Node.js. This means that importing functions from your Worker into your test files might exhibit different behaviour than you'd see at runtime if the functions rely on `workerd`-specific behaviour.
99
+
When using the [Vitest integration](/workers/testing/vitest-integration/),
100
+
your entire test suite runs in
101
+
[`workerd`](https://github.com/cloudflare/workerd), which is why it's possible
102
+
to unit test individual functions. By contrast, when using a different testing
103
+
framework to run tests via Miniflare, only your Worker itself is running in
104
+
[`workerd`](https://github.com/cloudflare/workerd)—your test files run in
105
+
Node.js. This means that importing functions from your Worker into your test
106
+
files might exhibit different behaviour than you'd see at runtime if the
107
+
functions rely on `workerd`-specific behaviour.
100
108
</Details>
101
109
102
110
## Interacting with Bindings
103
111
112
+
:::caution
113
+
114
+
Miniflare does not read [Wrangler's config file](/workers/wrangler/configuration), and so all bindings that your Worker uses need to be specified in the Miniflare API options.
115
+
116
+
:::
117
+
104
118
The `dispatchFetch()` API from Miniflare allows you to send requests to your Worker and assert that the correct response is returned, but sometimes you need to interact directly with bindings in tests. For use cases like that, Miniflare provides the [`getBindings()`](/workers/testing/miniflare/get-started/#reference) API. For instance, to access an environment variable in your tests, adapt the test file `src/index.test.js` as follows:
105
119
106
120
```diff lang="js" title="src/index.test.js"
@@ -127,7 +141,6 @@ describe("worker", () => {
127
141
128
142
You can also interact with local resources such as KV and R2, using the same API as you would from a Worker. For example, here's how you would interact with a KV namespace:
129
143
130
-
131
144
```diff lang="js" title="src/index.test.js"
132
145
...
133
146
describe("worker", () => {
@@ -168,7 +181,7 @@ new Miniflare({
168
181
});
169
182
```
170
183
171
-
This can be a bit cumbersome as your Worker grows. To help with thi, Miniflare can also crawl your module graph to automatically figure out which modules to include:
184
+
This can be a bit cumbersome as your Worker grows. To help with this, Miniflare can also crawl your module graph to automatically figure out which modules to include:
@@ -61,6 +59,8 @@ The `unstable_dev()` function has an `unstable_` prefix because the API is exper
61
59
62
60
`unstable_dev()` has no known bugs and is safe to use. If you discover any bugs, open a [GitHub issue](https://github.com/cloudflare/workers-sdk/issues/new/choose).
63
61
62
+
If you have been using `unstable_dev()` for integration testing and want to migrate to Cloudflare's Vitest integration, refer to the [Migrate from `unstable_dev` migration guide](/workers/testing/vitest-integration/get-started/migrate-from-unstable-dev/) for more information.
63
+
64
64
:::
65
65
66
66
### Constructor
@@ -374,8 +374,6 @@ The bindings supported by `getPlatformProxy` are:
374
374
375
375
For example, you might have the following file read by `getPlatformProxy`.
0 commit comments