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:
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,
});
});