Running One Test Over Many Cases

Kirsty’s ski length test checks one rule for five customers. In Python, @pytest.mark.parametrize feeds five (height, ability, expected) rows to test_ski_length, and pytest -v lists each row as its own test, such as test_ski_length[141-beginner-125]. The JavaScript port should keep one named result per case, so you can identify a failing row.

Give Each Case a Test

node:test has no parametrize decorator. It doesn’t need one, because test() is an ordinary function call that a for...of loop can make once per case. The loop sits inside describe(), also from node:test, which groups the tests under one heading.

Replace the contents of fitting.test.js, next to the provided fitting.js, with this port:

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

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

Array destructuring unpacks each row as Python’s tuple unpacking would, and the template literal names the test after its case, like pytest’s [141-beginner-125] ID. Destructuring doesn’t check the row’s length, though, so a missing value becomes undefined where Python would raise ValueError.

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

Become a Member to join the conversation.