Skip to content

Commit 907484f

Browse files
meixgruyadorno
authored andcommitted
assert: fix deepEqual always return true on URL
PR-URL: #50853 Fixes: #50836 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 03902eb commit 907484f

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
@@ -28,6 +28,7 @@ const {
2828
const { compare } = internalBinding('buffer');
2929
const assert = require('internal/assert');
3030
const { isError } = require('internal/util');
31+
const { isURL } = require('internal/url');
3132
const types = require('internal/util/types');
3233
const {
3334
isAnyArrayBuffer,
@@ -286,6 +287,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
286287
) {
287288
return false;
288289
}
290+
} else if (isURL(val1)) {
291+
if (!isURL(val2) || val1.href !== val2.href) {
292+
return false;
293+
}
289294
}
290295
return keyCheck(val1, val2, strict, memos, kNoIterator);
291296
}

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
@@ -1345,3 +1345,47 @@ test('Crypto', { skip: !hasCrypto }, async () => {
13451345
assertDeepAndStrictEqual(a, b);
13461346
}
13471347
});
1348+
1349+
// check URL
1350+
{
1351+
const a = new URL('http://foo');
1352+
const b = new URL('http://bar');
1353+
1354+
assertNotDeepOrStrict(a, b);
1355+
}
1356+
1357+
{
1358+
const a = new URL('http://foo');
1359+
const b = new URL('http://foo');
1360+
1361+
assertDeepAndStrictEqual(a, b);
1362+
}
1363+
1364+
{
1365+
const a = new URL('http://foo');
1366+
const b = new URL('http://foo');
1367+
a.bar = 1;
1368+
b.bar = 2;
1369+
assertNotDeepOrStrict(a, b);
1370+
}
1371+
1372+
{
1373+
const a = new URL('http://foo');
1374+
const b = new URL('http://foo');
1375+
a.bar = 1;
1376+
b.bar = 1;
1377+
assertDeepAndStrictEqual(a, b);
1378+
}
1379+
1380+
{
1381+
const a = new URL('http://foo');
1382+
const b = new URL('http://bar');
1383+
assert.throws(
1384+
() => assert.deepStrictEqual(a, b),
1385+
{
1386+
code: 'ERR_ASSERTION',
1387+
name: 'AssertionError',
1388+
message: /http:\/\/bar/
1389+
}
1390+
);
1391+
}

0 commit comments

Comments
 (0)