From f73e9a9e50bda883bac37f6d0ef33e357386cce9 Mon Sep 17 00:00:00 2001 From: Jo Franchetti Date: Tue, 9 Dec 2025 13:15:05 +0000 Subject: [PATCH 1/4] fix type error in stubbing example --- examples/tutorials/stubbing.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/tutorials/stubbing.md b/examples/tutorials/stubbing.md index 6c4048120..dd9e826cc 100644 --- a/examples/tutorials/stubbing.md +++ b/examples/tutorials/stubbing.md @@ -32,21 +32,23 @@ Here's a simple example demonstrating how to stub a function: import { assertEquals } from "jsr:@std/assert"; import { stub } from "jsr:@std/testing/mock"; -// Original function -function getUserName(id: number): string { - // In a real app, this might call a database - return "Original User"; -} +// Wrap dependencies so they can be stubbed safely from tests. +const deps = { + getUserName(id: number): string { + // In a real app, this might call a database + return "Original User"; + }, +}; // Function under test function greetUser(id: number): string { - const name = getUserName(id); + const name = deps.getUserName(id); return `Hello, ${name}!`; } Deno.test("greetUser with stubbed getUserName", () => { // Create a stub that returns a controlled value - const getUserNameStub = stub(globalThis, "getUserName", () => "Test User"); + const getUserNameStub = stub(deps, "getUserName", () => "Test User"); try { // Test with the stubbed function From cc95fbe9ee4e12686e645b77b8c40386ad88b4ed Mon Sep 17 00:00:00 2001 From: Jo Franchetti Date: Tue, 9 Dec 2025 14:12:46 +0000 Subject: [PATCH 2/4] underscore id since it is not used in the example --- examples/tutorials/stubbing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/tutorials/stubbing.md b/examples/tutorials/stubbing.md index dd9e826cc..27df77c4f 100644 --- a/examples/tutorials/stubbing.md +++ b/examples/tutorials/stubbing.md @@ -41,8 +41,8 @@ const deps = { }; // Function under test -function greetUser(id: number): string { - const name = deps.getUserName(id); +function greetUser(_id: number): string { + const name = deps.getUserName(_id); return `Hello, ${name}!`; } From dc31ab57f69b5008b2944ba4af05be441d55eef0 Mon Sep 17 00:00:00 2001 From: Jo Franchetti Date: Tue, 9 Dec 2025 18:25:59 +0000 Subject: [PATCH 3/4] fix broken examples --- examples/tutorials/stubbing.md | 37 +++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/examples/tutorials/stubbing.md b/examples/tutorials/stubbing.md index 27df77c4f..ab38dccc0 100644 --- a/examples/tutorials/stubbing.md +++ b/examples/tutorials/stubbing.md @@ -34,15 +34,15 @@ import { stub } from "jsr:@std/testing/mock"; // Wrap dependencies so they can be stubbed safely from tests. const deps = { - getUserName(id: number): string { + getUserName(_id: number): string { // In a real app, this might call a database return "Original User"; }, }; // Function under test -function greetUser(_id: number): string { - const name = deps.getUserName(_id); +function greetUser(id: number): string { + const name = deps.getUserName(id); return `Hello, ${name}!`; } @@ -138,21 +138,26 @@ import { returnsNext, stub } from "jsr:@std/testing/mock"; import { assertEquals } from "jsr:@std/assert"; Deno.test("stub with multiple return values", () => { + const dataService = { + fetchData: () => "original data", + }; + const fetchDataStub = stub( - globalThis, + dataService, "fetchData", // Return these values in sequence returnsNext(["first result", "second result", "third result"]), ); try { - assertEquals(fetchData(), "first result"); - assertEquals(fetchData(), "second result"); - assertEquals(fetchData(), "third result"); + assertEquals(dataService.fetchData(), "first result"); + assertEquals(dataService.fetchData(), "second result"); + assertEquals(dataService.fetchData(), "third result"); } finally { fetchDataStub.restore(); } }); + ``` ### Stubbing with implementation logic @@ -167,8 +172,12 @@ Deno.test("stub with custom implementation", () => { // Create a counter to track how many times the stub is called let callCount = 0; + const mathService = { + calculate: (a: number, b: number) => a + b, + }; + const calculateStub = stub( - globalThis, + mathService, "calculate", (a: number, b: number) => { callCount++; @@ -177,7 +186,7 @@ Deno.test("stub with custom implementation", () => { ); try { - const result = calculate(5, 10); + const result = mathService.calculate(5, 10); assertEquals(result, 25); // 5 + (10 * 2) assertEquals(callCount, 1); } finally { @@ -194,8 +203,12 @@ One of the most common uses of stubs is to replace API calls during testing: import { assertEquals } from "jsr:@std/assert"; import { stub } from "jsr:@std/testing/mock"; +const apiClient = { + fetch: globalThis.fetch, +}; + async function fetchUserData(id: string) { - const response = await fetch(`https://api.example.com/users/${id}`); + const response = await apiClient.fetch(`https://api.example.com/users/${id}`); if (!response.ok) { throw new Error(`Failed to fetch user: ${response.status}`); } @@ -208,9 +221,9 @@ Deno.test("fetchUserData with stubbed fetch", async () => { { status: 200, headers: { "Content-Type": "application/json" } }, ); - // Replace global fetch with a stubbed version + // Replace apiClient.fetch with a stubbed version const fetchStub = stub( - globalThis, + apiClient, "fetch", () => Promise.resolve(mockResponse), ); From 198b177482622f512577e2f7ebeca9c5acceb4f5 Mon Sep 17 00:00:00 2001 From: Jo Franchetti Date: Tue, 9 Dec 2025 18:26:44 +0000 Subject: [PATCH 4/4] fmt --- examples/tutorials/stubbing.md | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/tutorials/stubbing.md b/examples/tutorials/stubbing.md index ab38dccc0..756356a4c 100644 --- a/examples/tutorials/stubbing.md +++ b/examples/tutorials/stubbing.md @@ -157,7 +157,6 @@ Deno.test("stub with multiple return values", () => { fetchDataStub.restore(); } }); - ``` ### Stubbing with implementation logic