Skip to content

Commit 4584a83

Browse files
Merge branch 'main' into patch-1
2 parents bc16645 + 3c70a71 commit 4584a83

File tree

6 files changed

+37
-20
lines changed

6 files changed

+37
-20
lines changed

examples/tutorials/stubbing.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,23 @@ Here's a simple example demonstrating how to stub a function:
3232
import { assertEquals } from "jsr:@std/assert";
3333
import { stub } from "jsr:@std/testing/mock";
3434

35-
// Original function
36-
function getUserName(id: number): string {
37-
// In a real app, this might call a database
38-
return "Original User";
39-
}
35+
// Wrap dependencies so they can be stubbed safely from tests.
36+
const deps = {
37+
getUserName(_id: number): string {
38+
// In a real app, this might call a database
39+
return "Original User";
40+
},
41+
};
4042

4143
// Function under test
4244
function greetUser(id: number): string {
43-
const name = getUserName(id);
45+
const name = deps.getUserName(id);
4446
return `Hello, ${name}!`;
4547
}
4648

4749
Deno.test("greetUser with stubbed getUserName", () => {
4850
// Create a stub that returns a controlled value
49-
const getUserNameStub = stub(globalThis, "getUserName", () => "Test User");
51+
const getUserNameStub = stub(deps, "getUserName", () => "Test User");
5052

5153
try {
5254
// Test with the stubbed function
@@ -136,17 +138,21 @@ import { returnsNext, stub } from "jsr:@std/testing/mock";
136138
import { assertEquals } from "jsr:@std/assert";
137139

138140
Deno.test("stub with multiple return values", () => {
141+
const dataService = {
142+
fetchData: () => "original data",
143+
};
144+
139145
const fetchDataStub = stub(
140-
globalThis,
146+
dataService,
141147
"fetchData",
142148
// Return these values in sequence
143149
returnsNext(["first result", "second result", "third result"]),
144150
);
145151

146152
try {
147-
assertEquals(fetchData(), "first result");
148-
assertEquals(fetchData(), "second result");
149-
assertEquals(fetchData(), "third result");
153+
assertEquals(dataService.fetchData(), "first result");
154+
assertEquals(dataService.fetchData(), "second result");
155+
assertEquals(dataService.fetchData(), "third result");
150156
} finally {
151157
fetchDataStub.restore();
152158
}
@@ -165,8 +171,12 @@ Deno.test("stub with custom implementation", () => {
165171
// Create a counter to track how many times the stub is called
166172
let callCount = 0;
167173

174+
const mathService = {
175+
calculate: (a: number, b: number) => a + b,
176+
};
177+
168178
const calculateStub = stub(
169-
globalThis,
179+
mathService,
170180
"calculate",
171181
(a: number, b: number) => {
172182
callCount++;
@@ -175,7 +185,7 @@ Deno.test("stub with custom implementation", () => {
175185
);
176186

177187
try {
178-
const result = calculate(5, 10);
188+
const result = mathService.calculate(5, 10);
179189
assertEquals(result, 25); // 5 + (10 * 2)
180190
assertEquals(callCount, 1);
181191
} finally {
@@ -192,8 +202,12 @@ One of the most common uses of stubs is to replace API calls during testing:
192202
import { assertEquals } from "jsr:@std/assert";
193203
import { stub } from "jsr:@std/testing/mock";
194204

205+
const apiClient = {
206+
fetch: globalThis.fetch,
207+
};
208+
195209
async function fetchUserData(id: string) {
196-
const response = await fetch(`https://api.example.com/users/${id}`);
210+
const response = await apiClient.fetch(`https://api.example.com/users/${id}`);
197211
if (!response.ok) {
198212
throw new Error(`Failed to fetch user: ${response.status}`);
199213
}
@@ -206,9 +220,9 @@ Deno.test("fetchUserData with stubbed fetch", async () => {
206220
{ status: 200, headers: { "Content-Type": "application/json" } },
207221
);
208222

209-
// Replace global fetch with a stubbed version
223+
// Replace apiClient.fetch with a stubbed version
210224
const fetchStub = stub(
211-
globalThis,
225+
apiClient,
212226
"fetch",
213227
() => Promise.resolve(mockResponse),
214228
);

lint/rules/no-const-assign.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
tags: []
2+
tags: [recommended]
33
---
44

55
Disallows modifying a variable declared as `const`.

replacements.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"CLI_VERSION": "2.6.0"
2+
"CLI_VERSION": "2.6.1"
33
}

runtime/reference/cli/_commands_reference.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,13 +1572,16 @@
15721572
{
15731573
"name": "require",
15741574
"short": null,
1575+
"long": "require",
15751576
"required": false,
15761577
"help": "A list of CommonJS modules that will be executed before the main module",
15771578
"help_heading": null,
15781579
"usage": "--require <FILE>"
15791580
},
15801581
{
15811582
"name": "minimum-dependency-age",
1583+
"short": null,
1584+
"long": "minimum-dependency-age",
15821585
"required": false,
15831586
"help": "(Unstable) The age in minutes, ISO-8601 duration or RFC3339 absolute timestamp (e.g. '120' for two hours, 'P2D' for two days, '2025-09-16' for cutoff date, '2025-09-16T12:00:00+00:00' for cutoff time, '0' to disable)",
15841587
"help_heading": null,

sandboxes/expose_http.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ await sandbox.writeTextFile(
1212
"Deno.serve(() => new Response('Hello from Sandboxes'));",
1313
);
1414
const runtime = await sandbox.createJsRuntime({ entrypoint: "server.js" });
15-
const publicUrl = await sandbox.exposeHttp({ port: 8080 });
15+
const publicUrl = await sandbox.exposeHttp({ port: 8000 });
1616
console.log(publicUrl); // https://<random>.sandbox.deno.net
1717
```
1818

sandboxes/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "About Sandboxes"
2+
title: "Deno Sandboxes"
33
description: "Overview of the Sandboxes microVM platform on Deploy, including capabilities, security model, and ideal use cases."
44
---
55

0 commit comments

Comments
 (0)