Skip to content

Commit fe667be

Browse files
meixgaduh95
authored andcommitted
assert: fix deepEqual always return true on URL
PR-URL: #50853 Fixes: #50836 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 48bb87b commit fe667be

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

lib/internal/util/comparisons.js

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727

2828
const { compare } = internalBinding('buffer');
2929
const assert = require('internal/assert');
30+
const { isURL } = require('internal/url');
3031
const types = require('internal/util/types');
3132
const {
3233
isAnyArrayBuffer,
@@ -287,6 +288,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
287288
}
288289
} else if (isWeakMap(val1) || isWeakSet(val1)) {
289290
return false;
291+
} else if (isURL(val1)) {
292+
if (!isURL(val2) || val1.href !== val2.href) {
293+
return false;
294+
}
290295
}
291296

292297
return keyCheck(val1, val2, strict, memos, kNoIterator);

lib/internal/util/inspect.js

+10
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ function pathToFileUrlHref(filepath) {
166166
return internalUrl.pathToFileURL(filepath).href;
167167
}
168168

169+
function isURL(value) {
170+
internalUrl ??= require('internal/url');
171+
return typeof value.href === 'string' && value instanceof internalUrl.URL;
172+
}
173+
169174
const builtInObjects = new SafeSet(
170175
ArrayPrototypeFilter(
171176
ObjectGetOwnPropertyNames(globalThis),
@@ -1026,6 +1031,11 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
10261031
if (keys.length === 0 && protoProps === undefined) {
10271032
return base;
10281033
}
1034+
} else if (isURL(value) && !(recurseTimes > ctx.depth && ctx.depth !== null)) {
1035+
base = value.href;
1036+
if (keys.length === 0 && protoProps === undefined) {
1037+
return base;
1038+
}
10291039
} else {
10301040
if (keys.length === 0 && protoProps === undefined) {
10311041
if (isExternal(value)) {

test/parallel/test-assert-deep.js

+44
Original file line numberDiff line numberDiff line change
@@ -1346,3 +1346,47 @@ test('Comparing two different WeakSet instances', () => {
13461346
const weakSet2 = new WeakSet();
13471347
assertNotDeepOrStrict(weakSet1, weakSet2);
13481348
});
1349+
1350+
// check URL
1351+
{
1352+
const a = new URL('http://foo');
1353+
const b = new URL('http://bar');
1354+
1355+
assertNotDeepOrStrict(a, b);
1356+
}
1357+
1358+
{
1359+
const a = new URL('http://foo');
1360+
const b = new URL('http://foo');
1361+
1362+
assertDeepAndStrictEqual(a, b);
1363+
}
1364+
1365+
{
1366+
const a = new URL('http://foo');
1367+
const b = new URL('http://foo');
1368+
a.bar = 1;
1369+
b.bar = 2;
1370+
assertNotDeepOrStrict(a, b);
1371+
}
1372+
1373+
{
1374+
const a = new URL('http://foo');
1375+
const b = new URL('http://foo');
1376+
a.bar = 1;
1377+
b.bar = 1;
1378+
assertDeepAndStrictEqual(a, b);
1379+
}
1380+
1381+
{
1382+
const a = new URL('http://foo');
1383+
const b = new URL('http://bar');
1384+
assert.throws(
1385+
() => assert.deepStrictEqual(a, b),
1386+
{
1387+
code: 'ERR_ASSERTION',
1388+
name: 'AssertionError',
1389+
message: /http:\/\/bar/
1390+
}
1391+
);
1392+
}

0 commit comments

Comments
 (0)