Comparing Values and Objects in Assertions

Your first test checks Morag’s ski length. Kirsty’s next pytest test compares Morag’s whole fitting with an expected dict using one ==. The JavaScript port needs a different assertion, and the natural-looking one fails even when fit() is correct.

Port the Fitting Test

Here’s Kirsty’s test_fit, with Morag’s data built inside the test for now:

Language: Python
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,
    }

The line-by-line port swaps Python’s assert for assert() and == for ===. Replace the contents of fitting.test.js, beside fitting.js, with this port, and predict the result of node --test. You’ll put the full suite back together later:

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

It fails with exit code 1. The videos add --test-reporter=spec, but spec is already the default reporter, so plain node --test prints the same report. Durations vary, and one stack frame is wrapped for display. Read the AssertionError message and the fields at the end:

Language: Program Output
✖ fit (9.62859ms)
ℹ tests 1
ℹ suites 0
ℹ pass 0
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 17.16507

✖ failing tests:

test at fitting.test.js:5:1
✖ fit (9.62859ms)
  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'
  }

The message quotes only the expression’s first line, and actual: false shows that assert() received a bare false and never saw the two fittings. operator: '==' belongs to that check for a truthy value, one JavaScript treats as true in a condition. Your === had already run before assert() was called.

The cause is object identity. For objects, === asks whether both sides are the same object, like Python’s is. The literal in the test is always a new object, so the answer is false however well the properties match. Python’s == on dicts compares contents.

Compare Contents With deepStrictEqual()

For plain objects and arrays such as these fittings, assert.deepStrictEqual() compares contents and types at every level of nesting. Both sides must also be the same kind of object, so a class instance never equals a plain object with the same fields. Python’s 5.0 becomes 5 in JavaScript, since whole and decimal numbers share one type, Number. Here’s the fix:

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

Now node --test passes:

Language: Program Output
✔ fit (1.39707ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 11.03619

Because assert.deepStrictEqual() receives both values, its failure message names the property that differs. Change the expected skis to 160 and rerun to see this output, which is cut at the ... lines:

Language: Text
...
  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 + line is what fit() returned, and the - line is what you expected. Put 155 back afterward.

Pick the Assertion by What You Compare

pytest rewrites a plain assert so a failure shows both sides. Node’s node:assert/strict can’t rewrite your code, which is why the line-by-line port told you so little. It gives you named assertions instead, each saying what it compares:

pytest node:assert/strict
assert length == 155 assert.strictEqual(length, 155)
assert fitting == {...} (a dict or list) assert.deepStrictEqual(fitting, {...})
assert length > 0 assert.ok(length > 0)
rewritten failure showing both sides strictEqual() shows both values (155 !== 160), and deepStrictEqual() a + actual and - expected diff

Pass the actual value first and the expected one second, so the failure labels them right. strictEqual() compares like ===, except that it treats NaN as equal to NaN and -0 as different from 0, unlike Python’s ==.

assert.ok(), or its shorthand assert(), passes for any truthy value. Given a comparison such as length > 0, it receives only the resulting true or false, so its failure can’t show length. Use it only for checks with no expected value.

Watch out: if you import from plain "node:assert", you get the older assert.equal(), which uses loose ==, so assert.equal(155, "155") passes. Always import from "node:assert/strict".

In your JavaScript tests, import from node:assert/strict, use strictEqual() for a single value and deepStrictEqual() for an object or array, and pass the actual value first.

Next, in Test the Booking Line Parser, you’ll write tests for the customer object the booking site builds from a line of the bookings file.

Become a Member to join the conversation.