From 9f320023978209125af6678d7106435d020452ce Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 17 Jun 2024 09:30:24 -0400 Subject: [PATCH] assert,util: correct comparison when both contain same reference Co-authored-by: Chris Harvey <1362083+chharvey@users.noreply.github.com> PR-URL: https://github.com/nodejs/node/pull/53431 Refs: https://github.com/nodejs/node/issues/53423 Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Moshe Atlow Reviewed-By: Yagiz Nizipli Reviewed-By: Luigi Pinca --- lib/internal/util/comparisons.js | 3 ++- test/parallel/test-assert-deep.js | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index b5d2d071479dec..523307aa29851b 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -502,8 +502,9 @@ function setEquiv(a, b, strict, memo) { for (const val of b) { // Primitive values have already been handled above. if (typeof val === 'object' && val !== null) { - if (!setHasEqualElement(set, val, strict, memo)) + if (!a.has(val) && !setHasEqualElement(set, val, strict, memo)) { return false; + } } else if (!strict && !a.has(val) && !setHasEqualElement(set, val, strict, memo)) { diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index 28f6bedfb8f54f..e94be7af1e6f23 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -375,7 +375,11 @@ assertOnlyDeepEqual( new Map([[undefined, null], ['+000', 2n]]), new Map([[null, undefined], [false, '2']]), ); - +const xarray = ['x']; +assertDeepAndStrictEqual( + new Set([xarray, ['y']]), + new Set([xarray, ['y']]) +); assertOnlyDeepEqual( new Set([null, '', 1n, 5, 2n, false]), new Set([undefined, 0, 5n, true, '2', '-000'])