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
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`.
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.
18
+
This guide will show you how to set up [Miniflare](/workers/testing/miniflare)to test your Workers. Miniflare is a low-level API that allows you to fully control how your Workers are run and tested.
19
19
20
20
To use Miniflare, make sure you've installed the latest version of Miniflare v3:
21
21
@@ -41,9 +41,9 @@ pnpm add -D miniflare
41
41
42
42
The rest of this guide demonstrates concepts with the [`node:test`](https://nodejs.org/api/test.html) testing framework, but any testing framework can be used.
43
43
44
-
Miniflare is a low-level API that exposes a large variety of configuration options for running your Worker. In most cases, your tests will only need a subset of the available options, but you can refer to the [full API reference](/workers/testing/miniflare/get-started/#reference) to explore what's possible with Miniflare.
44
+
Miniflare is a low-level API that exposes a large variety of configuration options for running your Worker. In most cases, your tests will only need a subset of the available options, but you can refer to the [full API reference](/workers/testing/miniflare/get-started/#reference) to explore what is possible with Miniflare.
45
45
46
-
Before writing a test, you'll need to create a Worker. Since Miniflare is a low-level API that emulates the Cloudflare platform primitives, your Worker will need to be written in JavaScript or you'll need to [integrate your own build pipeline](#custom-builds) into your testing setup. Here's an example JavaScript-only Worker:
46
+
Before writing a test, you will need to create a Worker. Since Miniflare is a low-level API that emulates the Cloudflare platform primitives, your Worker will need to be written in JavaScript or you'll need to [integrate your own build pipeline](#custom-builds) into your testing setup. Here's an example JavaScript-only Worker:
47
47
48
48
```js title="src/index.js"
49
49
exportdefault {
@@ -53,7 +53,7 @@ export default {
53
53
};
54
54
```
55
55
56
-
Next, you'll need to create an initial test file:
56
+
Next, you will need to create an initial test file:
You should be able to run the above test via `node --test`
95
95
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.
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
99
When using the [Vitest integration](/workers/testing/vitest-integration/),
100
100
your entire test suite runs in
101
-
[`workerd`](https://github.com/cloudflare/workerd), which is why it's possible
101
+
[`workerd`](https://github.com/cloudflare/workerd), which is why it is possible
102
102
to unit test individual functions. By contrast, when using a different testing
103
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
104
+
[`workerd`](https://github.com/cloudflare/workerd) — your test files run in
105
105
Node.js. This means that importing functions from your Worker into your test
106
106
files might exhibit different behaviour than you'd see at runtime if the
107
107
functions rely on `workerd`-specific behaviour.
@@ -111,7 +111,7 @@ The highlighted lines of the test file above demonstrate how to set up Miniflare
111
111
112
112
:::caution
113
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.
114
+
Miniflare does not read [Wrangler's config file](/workers/wrangler/configuration). All bindings that your Worker uses need to be specified in the Miniflare API options.
115
115
116
116
:::
117
117
@@ -139,7 +139,7 @@ describe("worker", () => {
139
139
});
140
140
```
141
141
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:
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:
143
143
144
144
```diff lang="js" title="src/index.test.js"
145
145
...
@@ -193,7 +193,7 @@ new Miniflare({
193
193
194
194
## Custom builds
195
195
196
-
In many real-world cases, Workers are not written as plain JavaScript, but are instead written as multiple TypeScript files importing from npm packages etc... that are then bundled by a build tool. When testing your Worker via Miniflare directly you need to run this build tool before your tests. Exactly how this build is run will depend on the specific test framework you use, but for `node:test` it would likely be in a `setup()` hook. For example, if you use [Wrangler](/workers/wrangler/) to build and deploy your Worker, you could spawn a `wrangler build` command:
196
+
In many real-world cases, Workers are not written in plain JavaScript but instead consist of multiple TypeScript files that import from npm packages and other dependencies, which are then bundled by a build tool. When testing your Worker via Miniflare directly you need to run this build tool before your tests. Exactly how this build is run will depend on the specific test framework you use, but for `node:test` it would likely be in a `setup()` hook. For example, if you use [Wrangler](/workers/wrangler/) to build and deploy your Worker, you could spawn a `wrangler build` command like this:
Copy file name to clipboardExpand all lines: src/content/docs/workers/wrangler/api.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ Wrangler offers APIs to programmatically interact with your Cloudflare Workers.
18
18
19
19
## `unstable_startWorker`
20
20
21
-
This API exposes the internals of Wrangler's dev server, and allows you to customise how it runs. For example, you could use `unstable_startWorker()` to run integration tests against your Worker (the below example is with `node:test`, but this should work in any testing framework):
21
+
This API exposes the internals of Wrangler's dev server, and allows you to customise how it runs. For example, you could use `unstable_startWorker()` to run integration tests against your Worker. This example uses `node:test`, but should apply to any testing framework:
0 commit comments