Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit cb3efd5

Browse files
committed
Bump versions to 2.7.0, update CHANGELOG.md and docs
1 parent adb8171 commit cb3efd5

File tree

25 files changed

+513
-305
lines changed

25 files changed

+513
-305
lines changed

docs/CHANGELOG.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,112 @@
11
# 🚧 Changelog
22

3+
# 2.7.0
4+
5+
> ⚠️ **Miniflare's minimum supported Node.js version is now `16.13.0`.** This
6+
> was the first LTS release of Node.js 16.
7+
>
8+
> We recommend you use the latest Node.js version if possible, as Cloudflare
9+
> Workers use a very up-to-date version of V8. Consider using a Node.js version
10+
> manager such as https://volta.sh/ or https://github.com/nvm-sh/nvm.
11+
12+
### Features
13+
14+
- 🎉 Add support for easily **mocking outbound `fetch` requests**. See
15+
[🕸 Web Standards](https://miniflare.dev/core/standards#mocking-outbound-fetch-requests)
16+
for more details. Closes
17+
[issue #162](https://github.com/cloudflare/miniflare/issues/162), thanks
18+
[@william1616](https://github.com/william1616) for
19+
[the PR](https://github.com/cloudflare/miniflare/pull/293).
20+
21+
```js
22+
test("mocks fetch", async () => {
23+
// Get correctly set up `MockAgent`
24+
const fetchMock = getMiniflareFetchMock();
25+
// Throw when no matching mocked request is found
26+
fetchMock.disableNetConnect();
27+
// Mock request to https://example.com/thing
28+
const origin = fetchMock.get("https://example.com");
29+
origin
30+
.intercept({ method: "GET", path: "/thing" })
31+
.reply(200, "Mocked response!");
32+
33+
const res = await fetch("https://example.com/thing");
34+
const text = await res.text();
35+
expect(text).toBe("Mocked response!");
36+
});
37+
```
38+
39+
- 🚽 Add support to immediately invoke _("flush")_ scheduled Durable Object
40+
alarms in the [🤹 Jest Environment](https://miniflare.dev/testing/jest).
41+
Closes [issue #322](https://github.com/cloudflare/miniflare/issues/322),
42+
thanks [@robertcepa](https://github.com/robertcepa) and
43+
[@CraigglesO](https://github.com/CraigglesO) for
44+
[the PR](https://github.com/cloudflare/miniflare/pull/324).
45+
46+
```js
47+
test("flushes alarms", async () => {
48+
// Get Durable Object stub
49+
const env = getMiniflareBindings();
50+
const id = env.TEST_OBJECT.newUniqueId();
51+
const stub = env.TEST_OBJECT.get(id);
52+
53+
// Schedule Durable Object alarm
54+
await stub.fetch("http://localhost/");
55+
56+
// Flush all alarms...
57+
await flushMiniflareDurableObjectAlarms();
58+
// ...or specify an array of `DurableObjectId`s to flush
59+
await flushMiniflareDurableObjectAlarms([id]);
60+
});
61+
```
62+
63+
- 🪣 Add support for R2 bucket bindings to the
64+
[🤹 Jest Environment](https://miniflare.dev/testing/jest). Closes
65+
[issue #305](https://github.com/cloudflare/miniflare/issues/305), thanks
66+
[@Cerberus](https://github.com/Cerberus) for
67+
[the PR](https://github.com/cloudflare/miniflare/pull/306).
68+
- 2️⃣ Add support for Wrangler 2's `routes` property. Closes
69+
[issue #254](https://github.com/cloudflare/miniflare/issues/254), thanks
70+
[@jrencz](https://github.com/jrencz) for
71+
[the PR](https://github.com/cloudflare/miniflare/pull/307).
72+
- ⚠️ Upgrade [`undici`](https://github.com/nodejs/undici) to
73+
[`5.9.1`](https://github.com/nodejs/undici/releases/tag/v5.9.1). Thanks
74+
[@yusukebe](https://github.com/yusukebe) and
75+
[@cameron-robey](https://github.com/cameron-robey) for
76+
[the](https://github.com/cloudflare/miniflare/pull/320)
77+
[PRs](https://github.com/cloudflare/miniflare/pull/333).
78+
79+
### Fixes
80+
81+
- Return custom `Content-Encoding`s, closes
82+
[issue #312](https://github.com/cloudflare/miniflare/issues/312), thanks
83+
[@vlovich](https://github.com/vlovich).
84+
- Fix reading symlinked files from Miniflare's file-system storage. Closes
85+
[issue #318](https://github.com/cloudflare/miniflare/issues/318), thanks
86+
[@CraigglesO](https://github.com/CraigglesO) for
87+
[the PR](https://github.com/cloudflare/miniflare/pull/319).
88+
- Display all accessible addresses when listening on host `0.0.0.0`. Closes
89+
[issue cloudflare/wrangler2#1652](https://github.com/cloudflare/wrangler2/issues/1652),
90+
thanks [@Skye-31](https://github.com/Skye-31) for
91+
[the PR](https://github.com/cloudflare/miniflare/pull/332).
92+
- Fix unbounded recursion when calling `Date.now()`/`new Date()` without
93+
`--actual-time` flag. Closes
94+
[issue #314](https://github.com/cloudflare/miniflare/issues/314), thanks
95+
[@WalshyDev](https://github.com/WalshyDev) and
96+
[@AggressivelyMeows](https://github.com/AggressivelyMeows).
97+
- Preserve full path in `File#name` field. Thanks
98+
[@yusefnapora](https://github.com/yusefnapora) for
99+
[the PR](https://github.com/cloudflare/miniflare/pull/309).
100+
- Change underlying glob matching implementation to `picomatch`. Closes
101+
[issue #244](https://github.com/cloudflare/miniflare/issues/244), thanks
102+
[@jed](https://github.com/jed) and [@cometkim](https://github.com/cometkim)
103+
for [the PR](https://github.com/cloudflare/miniflare/pull/316).
104+
- Fix `NotSupportedError` when using the `NODE-ED25519` algorithm in recent
105+
versions of Node.js. Closes
106+
[issue #310](https://github.com/cloudflare/miniflare/issues/310), thanks
107+
[@yusefnapora](https://github.com/yusefnapora) for
108+
[the PR](https://github.com/cloudflare/miniflare/pull/311).
109+
3110
## 2.6.0
4111

5112
### Features

docs/src/content/core/standards.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,54 @@ Miniflare supports the following Web Standards in its sandbox:
3939
`rejectionhandled`
4040
- **Misc:** `structuredClone`, `navigator`
4141

42+
## Mocking Outbound `fetch` Requests
43+
44+
When using the API, Miniflare allows you to substitute custom `Response`s for
45+
`fetch()` calls using `undici`'s
46+
[`MockAgent` API](https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentgetorigin).
47+
This is useful for testing workers that make HTTP requests to other services. To
48+
enable `fetch` mocking, create a
49+
[`MockAgent`](https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentgetorigin)
50+
using the `createFetchMock()` function, then set this using the `fetchMock`
51+
option. If you're using the
52+
[🤹 Jest Environment](/testing/jest#mocking-outbound-fetch-requests), use the
53+
global `getMiniflareFetchMock()` function to obtain a correctly set-up
54+
[`MockAgent`](https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentgetorigin).
55+
56+
```js
57+
import { Miniflare } from "miniflare";
58+
import { createFetchMock } from "@miniflare/core";
59+
60+
// Create `MockAgent` and connect it to the `Miniflare` instance
61+
const fetchMock = createFetchMock();
62+
const mf = new Miniflare({
63+
script: `export default {
64+
async fetch(request, env, ctx) {
65+
const res = await fetch("https://example.com/thing");
66+
const text = await res.text();
67+
return new Response(\`response:\${text}\`);
68+
}
69+
}`,
70+
modules: true,
71+
fetchMock,
72+
});
73+
74+
// Throw when no matching mocked request is found
75+
// (see https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentdisablenetconnect)
76+
fetchMock.disableNetConnect();
77+
78+
// Mock request to https://example.com/thing
79+
// (see https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentgetorigin)
80+
const origin = fetchMock.get("https://example.com");
81+
// (see https://undici.nodejs.org/#/docs/api/MockPool?id=mockpoolinterceptoptions)
82+
origin
83+
.intercept({ method: "GET", path: "/thing" })
84+
.reply(200, "Mocked response!");
85+
86+
const res = await mf.dispatchFetch("http://localhost:8787/");
87+
console.log(await res.text()); // "response:Mocked response!"
88+
```
89+
4290
## Subrequests
4391

4492
To match the behaviour of the Workers runtime, Miniflare limits you to

docs/src/content/testing/jest.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export default {
212212
```
213213
214214
To access Durable Object storage in tests, use the
215-
`getMiniflareDurableObjectStorage` global method:
215+
`getMiniflareDurableObjectStorage()` global function:
216216
217217
```js
218218
test("increments count", async () => {
@@ -243,3 +243,56 @@ won't be a problem, but be aware you may have issues with unique `Symbol()`s or
243243
`instanceof`s.
244244
245245
</Aside>
246+
247+
To immediately invoke _("flush")_ scheduled Durable Object alarms, use the
248+
`flushMiniflareDurableObjectAlarms()` global function:
249+
250+
```js
251+
test("flushes alarms", async () => {
252+
// Get Durable Object stub
253+
const env = getMiniflareBindings();
254+
const id = env.TEST_OBJECT.newUniqueId();
255+
const stub = env.TEST_OBJECT.get(id);
256+
257+
// Schedule Durable Object alarm
258+
await stub.fetch("http://localhost/");
259+
260+
// Flush all alarms...
261+
await flushMiniflareDurableObjectAlarms();
262+
// ...or specify an array of `DurableObjectId`s to flush
263+
await flushMiniflareDurableObjectAlarms([id]);
264+
});
265+
```
266+
267+
## Mocking Outbound `fetch` Requests
268+
269+
Miniflare allows you to substitute custom `Response`s for `fetch()` calls using
270+
`undici`'s
271+
[`MockAgent` API](https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentgetorigin).
272+
This is useful for testing workers that make HTTP requests to other services. To
273+
obtain a correctly set-up
274+
[`MockAgent`](https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentgetorigin),
275+
use the `getMiniflareFetchMock()` global function.
276+
277+
```js
278+
test("mocks fetch", async () => {
279+
// Get correctly set up `MockAgent`
280+
const fetchMock = getMiniflareFetchMock();
281+
282+
// Throw when no matching mocked request is found
283+
// (see https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentdisablenetconnect)
284+
fetchMock.disableNetConnect();
285+
286+
// Mock request to https://example.com/thing
287+
// (see https://undici.nodejs.org/#/docs/api/MockAgent?id=mockagentgetorigin)
288+
const origin = fetchMock.get("https://example.com");
289+
// (see https://undici.nodejs.org/#/docs/api/MockPool?id=mockpoolinterceptoptions)
290+
origin
291+
.intercept({ method: "GET", path: "/thing" })
292+
.reply(200, "Mocked response!");
293+
294+
const res = await fetch("https://example.com/thing");
295+
const text = await res.text();
296+
expect(text).toBe("Mocked response!");
297+
});
298+
```

0 commit comments

Comments
 (0)