Skip to content

Commit

Permalink
assert: inspect getters
Browse files Browse the repository at this point in the history
While asserting two objects the descriptor is not taken into account.
Therefore getters will be triggered as such. This makes sure they
are also highlighted in the error message instead of potentially
looking identical while the return value of the getter is actually
different.

PR-URL: nodejs#25004
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
BridgeAR authored and refack committed Jan 10, 2019
1 parent fffc103 commit a14a897
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/internal/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ function inspectValue(val) {
breakLength: Infinity,
// Assert does not detect proxies currently.
showProxy: false,
sorted: true
sorted: true,
// Inspect getters as we also check them when comparing entries.
getters: true
}
);
}
Expand Down
24 changes: 23 additions & 1 deletion test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ function re(literals, ...values) {
customInspect: false,
maxArrayLength: Infinity,
breakLength: Infinity,
sorted: true
sorted: true,
getters: true
});
// Need to escape special characters.
result += str;
Expand Down Expand Up @@ -1049,3 +1050,24 @@ assert.throws(
});
assertDeepAndStrictEqual(a, b);
}

// Check getters.
{
const a = {
get a() { return 5; }
};
const b = {
get a() { return 6; }
};
assert.throws(
() => assert.deepStrictEqual(a, b),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
message: /a: \[Getter: 5]\n- a: \[Getter: 6]\n /
}
);

// The descriptor is not compared.
assertDeepAndStrictEqual(a, { a: 5 });
}

0 comments on commit a14a897

Please sign in to comment.