Skip to content

Commit 7fa175f

Browse files
BridgeARjasnell
authored andcommitted
assert: fix deepEqual inconsistencies
PR-URL: #14491 Fixes: #14441 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 7d95dc3 commit 7fa175f

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

lib/assert.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,15 @@ function innerDeepEqual(actual, expected, strict, memos) {
279279
position: 0
280280
};
281281
} else {
282-
if (memos.actual.has(actual)) {
283-
return memos.actual.get(actual) === memos.expected.get(expected);
282+
// We prevent up to two map.has(x) calls by directly retrieving the value
283+
// and checking for undefined. The map can only contain numbers, so it is
284+
// safe to check for undefined only.
285+
const expectedMemoA = memos.actual.get(actual);
286+
if (expectedMemoA !== undefined) {
287+
const expectedMemoB = memos.expected.get(expected);
288+
if (expectedMemoB !== undefined) {
289+
return expectedMemoA === expectedMemoB;
290+
}
284291
}
285292
memos.position++;
286293
}

test/parallel/test-assert-deep.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,26 @@ assertOnlyDeepEqual(
303303
new Set([undefined])
304304
);
305305

306+
// Circular structures
307+
{
308+
const a = {};
309+
const b = {};
310+
a.a = a;
311+
b.a = {};
312+
b.a.a = a;
313+
assertDeepAndStrictEqual(a, b);
314+
}
315+
316+
{
317+
const a = new Set();
318+
const b = new Set();
319+
const c = new Set();
320+
a.add(a);
321+
b.add(b);
322+
c.add(a);
323+
assertDeepAndStrictEqual(b, c);
324+
}
325+
306326
{
307327
const values = [
308328
123,

test/parallel/test-assert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ a.throws(makeBlock(thrower, TypeError), function(err) {
550550

551551
const h = {ref: g};
552552

553-
a.throws(makeBlock(a.deepEqual, f, h), /AssertionError/);
554-
a.throws(makeBlock(a.deepStrictEqual, f, h), /AssertionError/);
553+
a.doesNotThrow(makeBlock(a.deepEqual, f, h));
554+
a.doesNotThrow(makeBlock(a.deepStrictEqual, f, h));
555555
}
556556
// GH-7178. Ensure reflexivity of deepEqual with `arguments` objects.
557557
const args = (function() { return arguments; })();

0 commit comments

Comments
 (0)