Skip to content

Commit 42b8ba1

Browse files
committed
assert: fix deepEqual always return true on URL
1 parent 1858341 commit 42b8ba1

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

lib/internal/util/comparisons.js

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626

2727
const { compare } = internalBinding('buffer');
2828
const assert = require('internal/assert');
29+
const { isURL } = require('internal/url');
2930
const types = require('internal/util/types');
3031
const {
3132
isAnyArrayBuffer,
@@ -249,6 +250,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
249250
isNativeError(val2) ||
250251
val2 instanceof Error) {
251252
return false;
253+
} else if (isURL(val1)) {
254+
if (!isURL(val2) || val1.href !== val2.href) {
255+
return false;
256+
}
252257
}
253258
return keyCheck(val1, val2, strict, memos, kNoIterator);
254259
}

lib/internal/util/inspect.js

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

168+
function isURL(value) {
169+
internalUrl ??= require('internal/url');
170+
return internalUrl.isURL(value);
171+
}
172+
168173
const builtInObjects = new SafeSet(
169174
ArrayPrototypeFilter(
170175
ObjectGetOwnPropertyNames(globalThis),
@@ -1022,6 +1027,11 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
10221027
if (keys.length === 0 && protoProps === undefined) {
10231028
return base;
10241029
}
1030+
} else if (isURL(value) && !(recurseTimes > ctx.depth && ctx.depth !== null)) {
1031+
base = value.href;
1032+
if (keys.length === 0 && protoProps === undefined) {
1033+
return base;
1034+
}
10251035
} else {
10261036
if (keys.length === 0 && protoProps === undefined) {
10271037
if (isExternal(value)) {

test/parallel/test-assert-deep.js

+31
Original file line numberDiff line numberDiff line change
@@ -1220,3 +1220,34 @@ assert.throws(
12201220

12211221
assertNotDeepOrStrict(a, b);
12221222
}
1223+
1224+
// check URL
1225+
{
1226+
const a = new URL('http://foo');
1227+
const b = new URL('http://bar');
1228+
1229+
assertNotDeepOrStrict(a, b);
1230+
}
1231+
1232+
{
1233+
const a = new URL('http://foo');
1234+
const b = new URL('http://foo');
1235+
1236+
assertDeepAndStrictEqual(a, b);
1237+
}
1238+
1239+
{
1240+
const a = new URL('http://foo');
1241+
const b = new URL('http://foo');
1242+
a.bar = 1;
1243+
b.bar = 2;
1244+
assertNotDeepOrStrict(a, b);
1245+
}
1246+
1247+
{
1248+
const a = new URL('http://foo');
1249+
const b = new URL('http://foo');
1250+
a.bar = 1;
1251+
b.bar = 1;
1252+
assertDeepAndStrictEqual(a, b);
1253+
}

0 commit comments

Comments
 (0)