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

Porting Snowline Hire's Test Suite

Porting Snowline Hire’s Test Suite

Keep the fitting checks while changing their JavaScript spelling. Use beforeEach() for fresh Morag data, register one test per ski-length case, check the error with an arrow and a message regex, and compare the complete fitting record with assert.deepStrictEqual().

The unknown-ability matcher checks the error’s name and message, rather than its class. Python checks the exception class, including subclasses. The final out-of-stock test keeps the class check for the next exercise.

Save this first part of the suite as fitting.test.js, beside the supplied fitting.js in your existing ski-fitting folder:

Language: JavaScript
import assert from "node:assert/strict";
import { beforeEach, describe, test } from "node:test";
import { FittingError, fit, skiLength } from "./fitting.js";

let morag;

beforeEach(() => {
  morag = {
    name: "Morag Fraser", height: 168, weight: 62,
    boot: 6, ability: "intermediate",
  };
});

test("ski length rounds down to stock", () => {
  assert.strictEqual(skiLength(168, "intermediate"), 155);
});

describe("skiLength()", () => {
  const cases = [
    [168, "intermediate", 155],
    [141, "beginner", 125],
    [183, "expert", 175],
    [110, "beginner", 120],
    [210, "expert", 190],
  ];
  for (const [height, ability, expected] of cases) {
    test(`${height} cm, ${ability}: ${expected}`, () => {
      assert.strictEqual(skiLength(height, ability), expected);
    });
  }
});

test("unknown ability", () => {
  assert.throws(() => skiLength(170, "racer"), {
    name: "FittingError", message: /unknown ability: racer/,
  });
});

test("fit", () => {
  assert.deepStrictEqual(fit(morag), {
    name: "Morag Fraser", skis: 155,
    boots: 24.5, din: 5,
  });
});

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 Start the test file beside the module. Keep the discovered filename from your first test. The imports come first.

00:40 Import strict assertions and the test runner’s tools. In Python, a fixture supplies Morag. In JavaScript, the hook creates fresh data before each test. The callback parameter is a test context, not an injected fixture.

00:56 The first Python check compares the ski length with the expected number. Its JavaScript counterpart uses the assertion tool. It checks strict equality. Keep the test’s readable name in the report.

01:20 This check still asks whether Morag gets one hundred fifty-five centimeter skis, rather than changing the business rule during the port. Python’s parameter table describes five checks.

01:58 In JavaScript, put the same rows in an array inside the group.

02:53 The loop registers a separate test for each row. Destructuring gives that row’s height, ability, and expected length their names. The report can identify the customer-sized case that fails.

03:07 Python checks the exception type and searches its message. In JavaScript, give the assertion an arrow.

03:33 The assertion can run the operation itself. Match the custom name and search the message with a regular expression. This checks the error’s name and message, rather than its class.

03:45 Python checks the exception class, including subclasses. The final out-of-stock test keeps the class check for the next exercise. Python compares the fitting dictionaries by contents.

03:58 In JavaScript, inspect the record comparison. Use the deep strict comparison already taught for records. Morag’s expected result includes her name, ski length, boot size, and binding setting.

04:28 Keep all four fields. Object identity would ask a different question from the contract Kirsty wrote. Run the JavaScript test file with the readable reporter selected.

04:38 Eight tests pass.

05:19 Python’s matching first eight pass too.

05:43 Timings vary between runs. The group organizes five cases; it isn’t an extra test. The complete Python suite still has three more checks to port. Now check the suite itself in isolated copies.

06:49 Change floor rounding to nearest rounding in both modules. Which checks will fail? Your turn: pause the video and predict. Four checks fail in each language.

07:08 The first check, two length cases, and the fitting record catch the change. Python’s complete suite also fails its reservation check.

08:55 Keep the correctly rounded module and your eight-check JavaScript file. The port keeps the fitting checks, with the error-name boundary you saw. In the next exercise, Finish the Test Suite, you’ll complete the loading and stock checks from Kirsty’s Python suite.

09:09 Keep the existing checks while you add those final three.

Become a Member to join the conversation.