Skip to content

Commit f13520c

Browse files
meixgRafaelGSS
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 cf23a89 commit f13520c

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

lib/internal/util/comparisons.js

Lines changed: 5 additions & 0 deletions
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,
@@ -283,6 +284,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
283284
) {
284285
return false;
285286
}
287+
} else if (isURL(val1)) {
288+
if (!isURL(val2) || val1.href !== val2.href) {
289+
return false;
290+
}
286291
}
287292
return keyCheck(val1, val2, strict, memos, kNoIterator);
288293
}

lib/internal/util/inspect.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ function pathToFileUrlHref(filepath) {
170170
return internalUrl.pathToFileURL(filepath).href;
171171
}
172172

173+
function isURL(value) {
174+
internalUrl ??= require('internal/url');
175+
return typeof value.href === 'string' && value instanceof internalUrl.URL;
176+
}
177+
173178
const builtInObjects = new SafeSet(
174179
ArrayPrototypeFilter(
175180
ObjectGetOwnPropertyNames(globalThis),
@@ -1046,6 +1051,11 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
10461051
if (keys.length === 0 && protoProps === undefined) {
10471052
return base;
10481053
}
1054+
} else if (isURL(value) && !(recurseTimes > ctx.depth && ctx.depth !== null)) {
1055+
base = value.href;
1056+
if (keys.length === 0 && protoProps === undefined) {
1057+
return base;
1058+
}
10491059
} else {
10501060
if (keys.length === 0 && protoProps === undefined) {
10511061
if (isExternal(value)) {

test/parallel/test-assert-deep.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,3 +1318,47 @@ test('Crypto', { skip: !hasCrypto }, async () => {
13181318
assertDeepAndStrictEqual(a, b);
13191319
}
13201320
});
1321+
1322+
// check URL
1323+
{
1324+
const a = new URL('http://foo');
1325+
const b = new URL('http://bar');
1326+
1327+
assertNotDeepOrStrict(a, b);
1328+
}
1329+
1330+
{
1331+
const a = new URL('http://foo');
1332+
const b = new URL('http://foo');
1333+
1334+
assertDeepAndStrictEqual(a, b);
1335+
}
1336+
1337+
{
1338+
const a = new URL('http://foo');
1339+
const b = new URL('http://foo');
1340+
a.bar = 1;
1341+
b.bar = 2;
1342+
assertNotDeepOrStrict(a, b);
1343+
}
1344+
1345+
{
1346+
const a = new URL('http://foo');
1347+
const b = new URL('http://foo');
1348+
a.bar = 1;
1349+
b.bar = 1;
1350+
assertDeepAndStrictEqual(a, b);
1351+
}
1352+
1353+
{
1354+
const a = new URL('http://foo');
1355+
const b = new URL('http://bar');
1356+
assert.throws(
1357+
() => assert.deepStrictEqual(a, b),
1358+
{
1359+
code: 'ERR_ASSERTION',
1360+
name: 'AssertionError',
1361+
message: /http:\/\/bar/
1362+
}
1363+
);
1364+
}

0 commit comments

Comments
 (0)