Skip to content

Commit bf3abd8

Browse files
committed
address comments
1 parent 986b5e7 commit bf3abd8

File tree

4 files changed

+46
-42
lines changed

4 files changed

+46
-42
lines changed

src/content/docs/workers/testing/index.mdx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@ import { Render, LinkButton } from "~/components";
99

1010
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.
1111

12-
1312
<LinkButton href="/workers/testing/vitest-integration/get-started/write-your-first-test/">
1413
Write your first test
1514
</LinkButton>
1615

17-
1816
## Testing comparison matrix
1917

20-
| Feature | [Vitest integration](/workers/testing/vitest-integration) | [`unstable_startWorker()`](/workers/wrangler/api/#unstable_startworker) | [Miniflare's API](/workers/testing/miniflare) |
21-
| ----------------------------------------- | ----------- | ---------------- | --------------- |
22-
| Unit testing | |||
23-
| Integration testing | |||
24-
| Loading Wrangler configuration files | |||
25-
| Bindings directly in tests | |||
26-
| Isolated per-test storage | |||
27-
| Outbound request mocking | |||
28-
| Multiple Worker support | |||
29-
| Direct access to Durable Objects | |||
30-
| Run Durable Object alarms immediately | |||
31-
| List Durable Objects | |||
32-
| Testing service Workers | |||
18+
| Feature | [Vitest integration](/workers/testing/vitest-integration) | [`unstable_startWorker()`](/workers/wrangler/api/#unstable_startworker) | [Miniflare's API](/workers/testing/miniflare) |
19+
| ------------------------------------- | --------------------------------------------------------- | ----------------------------------------------------------------------- | --------------------------------------------- |
20+
| Unit testing | | | |
21+
| Integration testing | | | |
22+
| Loading Wrangler configuration files | | | |
23+
| Use bindings directly in tests | | | |
24+
| Isolated per-test storage | | | |
25+
| Outbound request mocking | | | |
26+
| Multiple Worker support | | | |
27+
| Direct access to Durable Objects | | | |
28+
| Run Durable Object alarms immediately | | | |
29+
| List Durable Objects | | | |
30+
| Testing service Workers | | | |
3331

3432
<Render file="testing-pages-functions" product="workers" />

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ description: Test multiple units of your Worker working together.
88
---
99

1010
import { Render } from "~/components";
11+
import { LinkButton } from "@astrojs/starlight/components";
1112

1213
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:
1314

@@ -84,32 +85,30 @@ This method is less recommended than `SELF` for integration tests because of its
8485

8586
:::
8687

87-
## Wrangler's `unstable_dev()` API
88+
## [Wrangler's `unstable_startWorker()` API](/workers/wrangler/api/#unstable_startworker)
8889

89-
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.
9095

9196
```js
9297
import assert from "node:assert";
93-
import { unstable_dev } from "wrangler";
98+
import { unstable_startWorker } from "wrangler";
9499

95-
const worker = await unstable_dev("./index.mjs");
100+
const worker = await unstable_startWorker({ config: "wrangler.json" });
96101
try {
97102
const response = await worker.fetch("/?a=1&b=2");
98103
assert.strictEqual(await response.text(), "3");
99104
} finally {
100-
await worker.stop();
105+
await worker.dispose();
101106
}
102107
```
103108

104-
:::note
105-
106-
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.
107-
108-
:::
109-
110-
## Miniflare's API
109+
## [Miniflare's API](/workers/testing/miniflare/writing-tests/)
111110

112-
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`.
113112

114113
```js
115114
import assert from "node:assert";

src/content/docs/workers/testing/miniflare/writing-tests.mdx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ For most users, Cloudflare recommends using the Workers Vitest integration for t
1313

1414
import { TabItem, Tabs, Details } from "~/components";
1515

16-
import { FileTree } from '@astrojs/starlight/components';
16+
import { FileTree } from "@astrojs/starlight/components";
1717

1818
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.
1919

@@ -81,7 +81,7 @@ describe("worker", () => {
8181
test("hello world", async () => {
8282
assert.strictEqual(
8383
await (await worker.dispatchFetch("http://example.com")).text(),
84-
"Hello World"
84+
"Hello World",
8585
);
8686
});
8787

@@ -96,11 +96,25 @@ You should be able to run the above test via `node --test`
9696
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.
9797

9898
<Details header="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.
100108
</Details>
101109

102110
## Interacting with Bindings
103111

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+
104118
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:
105119

106120
```diff lang="js" title="src/index.test.js"
@@ -127,7 +141,6 @@ describe("worker", () => {
127141

128142
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:
129143

130-
131144
```diff lang="js" title="src/index.test.js"
132145
...
133146
describe("worker", () => {
@@ -168,7 +181,7 @@ new Miniflare({
168181
});
169182
```
170183

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:
172185

173186
```js
174187
new Miniflare({
@@ -190,7 +203,3 @@ before(() => {
190203
});
191204
});
192205
```
193-
194-
195-
196-

src/content/docs/workers/wrangler/api.mdx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("worker", () => {
3535
test("hello world", async () => {
3636
assert.strictEqual(
3737
await (await worker.fetch("http://example.com")).text(),
38-
"Hello world"
38+
"Hello world",
3939
);
4040
});
4141

@@ -45,8 +45,6 @@ describe("worker", () => {
4545
});
4646
```
4747

48-
49-
5048
## `unstable_dev`
5149

5250
Start an HTTP server for testing your Worker.
@@ -61,6 +59,8 @@ The `unstable_dev()` function has an `unstable_` prefix because the API is exper
6159

6260
`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).
6361

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+
6464
:::
6565

6666
### Constructor
@@ -374,8 +374,6 @@ The bindings supported by `getPlatformProxy` are:
374374

375375
For example, you might have the following file read by `getPlatformProxy`.
376376

377-
378-
379377
<WranglerConfig>
380378

381379
```toml

0 commit comments

Comments
 (0)