Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Replacing the Stock System With t.mock

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:

Language: JavaScript Filename: 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:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Snowline Hire’s reservation test needs to check the equipment requested for Morag, without taking anything from the shop. You’ll replace the stock method, check its calls, and keep that replacement from changing another test.

00:13 The same booking needs 155 centimeter skis and 24.5 boots. The supplied reservation requests those two equipment codes in order.

00:54 Python’s fixture returns Morag’s fresh dictionary. In JavaScript, beforeEach() assigns the same fresh object before every test.

01:25 In Python, Kirsty creates a Mock() and installs it on the inventory object with monkeypatch. Calling the reservation records the requests without changing stock.

01:38 The expected calls name the skis, then the boots. After this test, monkeypatch restores the original method. The neighboring stock test can check its real refusal.

02:34 In JavaScript, the built-in test module supplies an imported mock tracker. Its method tool replaces a named method on an object and returns the recorded function.

02:45 Its call records live under .mock.calls, with each call’s inputs in an .arguments array. The exported inventory is still the same object; its method can change.

02:58 The empty arrow body does no stock work when the reservation calls it. The returned hold function has a call history under .mock.calls.

03:07 Each record’s .arguments property is an array of that call’s inputs. The familiar .map() method makes an array of those argument arrays.

03:20 Compare it with the nested expected array to check the ski request followed by the boot request.

03:42 In ski-fitting, run this command. Its reporter option selects the readable report. The next test expects the real stock method to refuse unavailable skis. Will both tests pass?

03:55 Your turn: pause the video and predict.

04:04 The reservation test passes.

04:08 The out-of-stock test fails. It reports a missing expected exception.

04:16 The imported tracker keeps its replacement after the reservation test ends. The stock check therefore reaches the fake method, which returns normally. That tracker restores its methods when you call mock.restoreAll() explicitly.

04:31 Python’s requesting test had cleanup; the imported JavaScript tracker needs an owner for that lifetime. Receive the test context as t, then create the replacement through t.mock.

04:47 This tracker belongs to the reservation test. When that test ends, the context restores the original method automatically. The reservation passes. The stock check passes too.

05:01 Timings vary between runs. For a count, .mock.callCount() counts the recorded calls. For a stand-alone function, t.mock.fn() creates a tracked mock without replacing an object method.

05:17 Both use the same call-history tools. Watch out: imported bindings can’t be reassigned, and module mocking needs the --experimental-test-module-mocks flag; without it the module tool is unavailable.

05:34 Prefer collaborators supplied as objects or parameters, as this reservation uses inventory. You changed the object’s method property, while keeping its imported binding intact.

05:48 Keep each replacement inside the test that owns it, and check the requests it recorded. In the next exercise, Test the Reservation, you’ll write reservation checks with the stock system replaced.

Become a Member to join the conversation.