Snowline Hire’s first JavaScript test checks a ski length. Kirsty’s next test checks Morag’s whole fitting: her name, skis, boots, and binding setting. That matters because a correct ski length alone doesn’t mean the booking site will hand her the right equipment. You need an assertion that checks the complete result.
Choose What to Check
In Python, pytest rewrites a plain assert so that a failure can show the values in the expression. Node’s built-in assertion module offers named functions instead. Each name tells you what kind of check it performs. Import assert from "node:assert/strict" to keep comparisons strict about types.
For a single value, keep using assert.strictEqual(actual, expected), as in your first test. Put the value your code produced first and the value you require second, so a failure labels them correctly. For an invariant, a condition that should always hold, use assert.ok():
import assert from "node:assert/strict";
import { skiLength } from "./fitting.js";
assert.strictEqual(skiLength(168, "intermediate"), 155);
assert.ok(skiLength(168, "intermediate") > 0);
Both checks pass without printing anything. Like Python’s assert condition, assert.ok() requires a truthy value, one JavaScript treats as true in a condition. The languages differ in which values are truthy, so use an explicit comparison here. The positive-length check expresses a broad requirement, but it can’t catch a wrong positive length. Keep the exact-value check when the rule specifies a length.
Watch out: importing from plain "node:assert" gives the older assert.equal(), which uses loose ==, so assert.equal(155, "155") passes despite the different types. Always use "node:assert/strict" for these tests.
Compare the Whole Fitting
Kirsty’s Python test compares the contents of the returned dict with those of the expected dict. Save this as test_fitting.py beside the provided fitting.py, with Morag’s data inside the test for now:
from fitting import fit
def test_fit():
morag = {
"name": "Morag Fraser", "height": 168, "weight": 62,
"boot": 6, "ability": "intermediate",
}
assert fit(morag) == {
"name": "Morag Fraser", "skis": 155,
"boots": 24.5, "din": 5.0,
}
Python’s == compares the dicts’ contents. In the JavaScript port, the familiar strict comparison is ===. Calling assert(condition) is shorthand for assert.ok(condition), which checks truthiness. Replace fitting.test.js with this translation, keeping the provided fitting.js beside it. Predict whether the fitting test will pass before running node --test:
fitting.test.js
import assert from "node:assert/strict";
import { test } from "node:test";
import { fit } from "./fitting.js";
test("fit", () => {
const morag = {
name: "Morag Fraser", height: 168, weight: 62,
boot: 6, ability: "intermediate",
};
assert(fit(morag) === {
name: "Morag Fraser", skis: 155,
boots: 24.5, din: 5,
});
});
The test fails even though fit() is correct. Below is the complete failing-test diagnostic, after the summary reporting one failed test. Your test durations vary, and the path here represents your project folder. The first stack frame wraps onto two lines for display. The diagnostic’s operator: '==' labels the assertion’s truthiness check. It doesn’t mean your === comparison became loose equality:
✖ failing tests:
test at fitting.test.js:5:1
✖ fit (11.418635ms)
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert(fit(morag) === {
at TestContext.<anonymous>
(file:///Users/you/ski-fitting/fitting.test.js:10:3)
at Test.runInAsyncScope (node:async_hooks:226:14)
at Test.run (node:internal/test_runner/test:1402:25)
at Test.start (node:internal/test_runner/test:1262:17)
at startSubtestAfterBootstrap (node:internal/test_runner/harness:387:17) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '==',
diff: 'simple'
}
Read actual: false and expected: true: the assertion received the result of ===, rather than the two fitting objects. Object identity asks whether two references point to the same object. That’s what === checks for objects. The expected object literal, written directly with {...}, creates a separate object, so the comparison is false even when every property matches. Python’s dict equality doesn’t make that identity check.
Check Contents With Types
For arrays and plain data objects like this fitting, use assert.deepStrictEqual() to compare contents, including types at every nested level. Unlike assert.ok(actual === expected), it receives both values and can tell you which property differs. JavaScript’s number type represents Python’s 5.0 here as 5.
Replace the failed test with this version, passing the fitting itself first and the expected object second:
fitting.test.js
import assert from "node:assert/strict";
import { test } from "node:test";
import { fit } from "./fitting.js";
test("fit", () => {
const morag = {
name: "Morag Fraser", height: 168, weight: 62,
boot: 6, ability: "intermediate",
};
assert.deepStrictEqual(fit(morag), {
name: "Morag Fraser", skis: 155,
boots: 24.5, din: 5,
});
});
Run node --test again: the fitting test passes. To explore its failure message, change the expected ski length to 160 and rerun the tests. This excerpt shows the comparison, with ... lines marking omitted parts of the test report and stack trace:
...
AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected
{
boots: 24.5,
din: 5,
name: 'Morag Fraser',
+ skis: 155
- skis: 160
}
...
The diff marks actual values with + and expected values with -. You can read the differing property directly, without reducing the whole comparison to false. Restore 155 afterward so your test checks Snowline Hire’s rule.
Test the Booking Line Parser asks you to write tests for the customer object the booking site builds from a bookings file line.