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/integration-testing.mdx
+20-27Lines changed: 20 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,26 +5,25 @@ sidebar:
5
5
order: 3
6
6
head: []
7
7
description: Test multiple units of your Worker working together.
8
-
9
8
---
10
9
11
-
import { Render } from"~/components"
10
+
import { Render } from"~/components";
12
11
13
12
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:
14
13
15
14
```js
16
15
exportfunctionadd(a, b) {
17
-
return a + b;
16
+
return a + b;
18
17
}
19
18
20
19
exportdefault {
21
-
asyncfetch(request) {
22
-
consturl=newURL(request.url);
23
-
consta=parseInt(url.searchParams.get("a"));
24
-
constb=parseInt(url.searchParams.get("b"));
25
-
returnnewResponse(add(a, b));
26
-
}
27
-
}
20
+
asyncfetch(request) {
21
+
consturl=newURL(request.url);
22
+
consta=parseInt(url.searchParams.get("a"));
23
+
constb=parseInt(url.searchParams.get("b"));
24
+
returnnewResponse(add(a, b));
25
+
},
26
+
};
28
27
```
29
28
30
29
An integration test for this Worker might look like the following example:
Instead of running the Worker-under-test in the same Worker as the test runner like `SELF`, this example defines the Worker-under-test as an *auxiliary* Worker. This means the Worker runs in a separate isolate to the test runner, with a different global scope. The Worker-under-test runs in an environment closer to production, but Vite transformations and hot-module-reloading aren't applied to the Worker—you must compile your TypeScript to JavaScript beforehand.
77
+
Instead of running the Worker-under-test in the same Worker as the test runner like `SELF`, this example defines the Worker-under-test as an _auxiliary_ Worker. This means the Worker runs in a separate isolate to the test runner, with a different global scope. The Worker-under-test runs in an environment closer to production, but Vite transformations and hot-module-reloading aren't applied to the Worker—you must compile your TypeScript to JavaScript beforehand.
79
78
80
79
Auxiliary Workers cannot be configured from `wrangler.toml` files. You must use Miniflare [`WorkerOptions`](https://github.com/cloudflare/workers-sdk/tree/main/packages/miniflare#interface-workeroptions) in `vitest.config.ts`.
81
80
82
81
:::note
83
82
84
-
85
83
This method is less recommended than `SELF` for integration tests because of its developer experience. However, it can be useful when you are testing multiple Workers. You can define multiple Workers by different names in `vitest.config.ts` and reference them via `env`.
86
84
87
-
88
85
:::
89
86
90
87
## Wrangler's `unstable_dev()` API
@@ -97,51 +94,47 @@ import { unstable_dev } from "wrangler";
97
94
98
95
constworker=awaitunstable_dev("./index.mjs");
99
96
try {
100
-
constresponse=awaitworker.fetch("/?a=1&b=2");
101
-
assert.strictEqual(awaitresponse.text(), "3");
97
+
constresponse=awaitworker.fetch("/?a=1&b=2");
98
+
assert.strictEqual(awaitresponse.text(), "3");
102
99
} finally {
103
-
awaitworker.stop();
100
+
awaitworker.stop();
104
101
}
105
102
```
106
103
107
104
:::note
108
105
109
-
110
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.
111
107
112
-
113
108
:::
114
109
115
110
## Miniflare's API
116
111
117
-
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](https://github.com/cloudflare/workers-sdk/blob/main/packages/miniflare/README.md) 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.
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.
If you have been using the test environments from Miniflare 2 for integration testing and want to migrate to Cloudflare's Vitest integration, refer to the [Migrate from Miniflare 2 migration guide](/workers/testing/vitest-integration/get-started/migrate-from-miniflare-2/) for more information.
Miniflare supports both the traditional `service-worker` and the newer `modules` formats for writing workers. To use the `modules` format, enable it with:
11
+
12
+
```js
13
+
constmf=newMiniflare({
14
+
modules:true,
15
+
});
16
+
```
17
+
18
+
You can then use `modules` worker scripts like the following:
0 commit comments