Skip to content

Commit fe667be

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

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

‎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
constassert=require('internal/assert');
30+
const{ isURL }=require('internal/url');
3031
consttypes=require('internal/util/types');
3132
const{
3233
isAnyArrayBuffer,
@@ -287,6 +288,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
287288
}
288289
}elseif(isWeakMap(val1)||isWeakSet(val1)){
289290
returnfalse;
291+
}elseif(isURL(val1)){
292+
if(!isURL(val2)||val1.href!==val2.href){
293+
returnfalse;
294+
}
290295
}
291296

292297
returnkeyCheck(val1,val2,strict,memos,kNoIterator);

‎lib/internal/util/inspect.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ function pathToFileUrlHref(filepath) {
166166
returninternalUrl.pathToFileURL(filepath).href;
167167
}
168168

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

‎test/parallel/test-assert-deep.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,3 +1346,47 @@ test('Comparing two different WeakSet instances', () => {
13461346
constweakSet2=newWeakSet();
13471347
assertNotDeepOrStrict(weakSet1,weakSet2);
13481348
});
1349+
1350+
// check URL
1351+
{
1352+
consta=newURL('http://foo');
1353+
constb=newURL('http://bar');
1354+
1355+
assertNotDeepOrStrict(a,b);
1356+
}
1357+
1358+
{
1359+
consta=newURL('http://foo');
1360+
constb=newURL('http://foo');
1361+
1362+
assertDeepAndStrictEqual(a,b);
1363+
}
1364+
1365+
{
1366+
consta=newURL('http://foo');
1367+
constb=newURL('http://foo');
1368+
a.bar=1;
1369+
b.bar=2;
1370+
assertNotDeepOrStrict(a,b);
1371+
}
1372+
1373+
{
1374+
consta=newURL('http://foo');
1375+
constb=newURL('http://foo');
1376+
a.bar=1;
1377+
b.bar=1;
1378+
assertDeepAndStrictEqual(a,b);
1379+
}
1380+
1381+
{
1382+
consta=newURL('http://foo');
1383+
constb=newURL('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)