Skip to content

Commit a9ab28d

Browse files
BridgeARMylesBorins
authored andcommitted
assert: inspect getters
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: #25004 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 5eb5d1d commit a9ab28d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/internal/assert.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ function inspectValue(val) {
5656
breakLength: Infinity,
5757
// Assert does not detect proxies currently.
5858
showProxy: false,
59-
sorted: true
59+
sorted: true,
60+
// Inspect getters as we also check them when comparing entries.
61+
getters: true
6062
}
6163
);
6264
}

test/parallel/test-assert-deep.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ function re(literals, ...values) {
2424
customInspect: false,
2525
maxArrayLength: Infinity,
2626
breakLength: Infinity,
27-
sorted: true
27+
sorted: true,
28+
getters: true
2829
});
2930
// Need to escape special characters.
3031
result += str;
@@ -1049,3 +1050,24 @@ assert.throws(
10491050
});
10501051
assertDeepAndStrictEqual(a, b);
10511052
}
1053+
1054+
// Check getters.
1055+
{
1056+
const a = {
1057+
get a() { return 5; }
1058+
};
1059+
const b = {
1060+
get a() { return 6; }
1061+
};
1062+
assert.throws(
1063+
() => assert.deepStrictEqual(a, b),
1064+
{
1065+
code: 'ERR_ASSERTION',
1066+
name: 'AssertionError [ERR_ASSERTION]',
1067+
message: /a: \[Getter: 5]\n- a: \[Getter: 6]\n /
1068+
}
1069+
);
1070+
1071+
// The descriptor is not compared.
1072+
assertDeepAndStrictEqual(a, { a: 5 });
1073+
}

0 commit comments

Comments
 (0)