Skip to main content

Stub functions

Test stubs are an extension of spys that also allows you to replace the method's internal behavior for testing purposes. In this example, we will demonstrate how we can use stubs to only test the desired behaviour.

Edit on Github
import { assertSpyCalls, returnsNext, stub } from "jsr:@std/testing/mock";
import { assertThrows } from "jsr:@std/assert";

type User = {
  id: number;
  name: string;
};

function getUserById(id: number): User | undefined {
Actual database call would be here
  return { id, name: "Ada Lovelace" };
}

const database = { getUserById };

class UserRepository {
  static findOrThrow(id: number): User {
    const user = database.getUserById(id);
    if (!user) {
      throw new Error("User not found");
    }
    return user;
  }
}
As we want to test the `findOrThrow` method, we don't actually need to access the database
Deno.test("findOrThrow method throws when the user was not found", () => {
Stub the `getUserById` function to return `undefined` when called.
  using dbStub = stub(database, "getUserById", returnsNext([undefined]));
We expect this function call to throw an error
  assertThrows(() => UserRepository.findOrThrow(1), Error, "User not found");
  assertSpyCalls(dbStub, 1);
});

Run this example locally using the Deno CLI:

deno run https://docs.deno.com/examples/stubs.ts