Skip to content

Commit a8149c4

Browse files
BridgeARtniessen
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 8a53897 commit a8149c4

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
@@ -271,8 +271,15 @@ function innerDeepEqual(actual, expected, strict, memos) {
271271
position: 0
272272
};
273273
} else {
274-
if (memos.actual.has(actual)) {
275-
return memos.actual.get(actual) === memos.expected.get(expected);
274+
// We prevent up to two map.has(x) calls by directly retrieving the value
275+
// and checking for undefined. The map can only contain numbers, so it is
276+
// safe to check for undefined only.
277+
const expectedMemoA = memos.actual.get(actual);
278+
if (expectedMemoA !== undefined) {
279+
const expectedMemoB = memos.expected.get(expected);
280+
if (expectedMemoB !== undefined) {
281+
return expectedMemoA === expectedMemoB;
282+
}
276283
}
277284
memos.position++;
278285
}

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
@@ -578,8 +578,8 @@ a.throws(makeBlock(thrower, TypeError), function(err) {
578578

579579
const h = { ref: g };
580580

581-
a.throws(makeBlock(a.deepEqual, f, h), /AssertionError/);
582-
a.throws(makeBlock(a.deepStrictEqual, f, h), /AssertionError/);
581+
a.doesNotThrow(makeBlock(a.deepEqual, f, h));
582+
a.doesNotThrow(makeBlock(a.deepStrictEqual, f, h));
583583
}
584584
// GH-7178. Ensure reflexivity of deepEqual with `arguments` objects.
585585
const args = (function() { return arguments; })();

0 commit comments

Comments
 (0)