Skip to main content

Module assert_diff

Module assert_diff 

Source
Expand description

The structural diff Node prints inside an AssertionError message.

A failing deepStrictEqual/strictEqual in Node does not read { a: 1 } !== { a: 2 }; it renders both operands with util.inspect and prints a line-oriented diff of the two renderings, marking lines only in actual with + and lines only in expected with -:

Expected values to be strictly deep-equal:
+ actual - expected

  {
    a: 1,
+   b: 2
-   b: 3
  }

This is a port of lib/internal/assert/myers_diff.js and the createErrDiff half of lib/internal/assert/assertion_error.js. The diff itself is Myers’ O(ND) algorithm; the printer is the part with all the observable detail (which lines are context, when a long identical run collapses to ..., and the ... Skipped lines note that then appears in the header).

Colors are never emitted: the harness compares captured stdout, which Node itself renders uncolored when stderr is not a TTY.

Functions§

create_err_diff
Build the whole AssertionError message for the comparisons that carry a diff (strictEqual, deepStrictEqual, partialDeepStrictEqual).
inspect_operand
Render a value for the assert messages that show ONE operand rather than a diff (notDeepStrictEqual, deepEqual, notDeepEqual). These use the same expanded, sorted rendering as the diff body, not console.log’s.