Replacing the Stock System With t.mock
Replace the stock system’s method while checking the reservation, then let the test context restore it. Python’s monkeypatch restores changes after the requesting test; the mock imported from node:test keeps replacements until mock.restoreAll() is called. A later test can receive the fake method if you leave it in place.
Use the callback’s test context, conventionally named t, and t.mock.method(). Its final argument supplies the replacement function; the empty arrow body here does no stock work. The returned mock records calls in .mock.calls. Each call’s .arguments is an array, so .map() produces the argument arrays to compare:
fitting.test.js
import assert from "node:assert/strict";
import { beforeEach, test } from "node:test";
import { FittingError, inventory, reserve } from "./fitting.js";
let morag;
beforeEach(() => {
morag = {
name: "Morag Fraser", height: 168, weight: 62,
boot: 6, ability: "intermediate",
};
});
test("reserve holds skis and boots", (t) => {
const hold = t.mock.method(inventory, "hold", () => {});
reserve(morag);
assert.deepStrictEqual(
hold.mock.calls.map((c) => c.arguments),
[["SKI-155"], ["BOOT-24.5"]],
);
});
test("out of stock", () => {
assert.throws(() => inventory.hold("SKI-190"), FittingError);
});
Run the file in your existing ski-fitting folder. The reporter option selects the readable report shown in the video: