mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
assert: fix deepEqual always return true on URL
PR-URL: https://github.com/nodejs/node/pull/50853 Fixes: https://github.com/nodejs/node/issues/50836 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
@@ -27,6 +27,7 @@ const {
|
||||
|
||||
const { compare } = internalBinding('buffer');
|
||||
const assert = require('internal/assert');
|
||||
const { isURL } = require('internal/url');
|
||||
const types = require('internal/util/types');
|
||||
const {
|
||||
isAnyArrayBuffer,
|
||||
@@ -287,6 +288,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
|
||||
}
|
||||
} else if (isWeakMap(val1) || isWeakSet(val1)) {
|
||||
return false;
|
||||
} else if (isURL(val1)) {
|
||||
if (!isURL(val2) || val1.href !== val2.href) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return keyCheck(val1, val2, strict, memos, kNoIterator);
|
||||
|
||||
@@ -166,6 +166,11 @@ function pathToFileUrlHref(filepath) {
|
||||
return internalUrl.pathToFileURL(filepath).href;
|
||||
}
|
||||
|
||||
function isURL(value) {
|
||||
internalUrl ??= require('internal/url');
|
||||
return typeof value.href === 'string' && value instanceof internalUrl.URL;
|
||||
}
|
||||
|
||||
const builtInObjects = new SafeSet(
|
||||
ArrayPrototypeFilter(
|
||||
ObjectGetOwnPropertyNames(globalThis),
|
||||
@@ -1026,6 +1031,11 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
|
||||
if (keys.length === 0 && protoProps === undefined) {
|
||||
return base;
|
||||
}
|
||||
} else if (isURL(value) && !(recurseTimes > ctx.depth && ctx.depth !== null)) {
|
||||
base = value.href;
|
||||
if (keys.length === 0 && protoProps === undefined) {
|
||||
return base;
|
||||
}
|
||||
} else {
|
||||
if (keys.length === 0 && protoProps === undefined) {
|
||||
if (isExternal(value)) {
|
||||
|
||||
@@ -1346,3 +1346,47 @@ test('Comparing two different WeakSet instances', () => {
|
||||
const weakSet2 = new WeakSet();
|
||||
assertNotDeepOrStrict(weakSet1, weakSet2);
|
||||
});
|
||||
|
||||
// check URL
|
||||
{
|
||||
const a = new URL('http://foo');
|
||||
const b = new URL('http://bar');
|
||||
|
||||
assertNotDeepOrStrict(a, b);
|
||||
}
|
||||
|
||||
{
|
||||
const a = new URL('http://foo');
|
||||
const b = new URL('http://foo');
|
||||
|
||||
assertDeepAndStrictEqual(a, b);
|
||||
}
|
||||
|
||||
{
|
||||
const a = new URL('http://foo');
|
||||
const b = new URL('http://foo');
|
||||
a.bar = 1;
|
||||
b.bar = 2;
|
||||
assertNotDeepOrStrict(a, b);
|
||||
}
|
||||
|
||||
{
|
||||
const a = new URL('http://foo');
|
||||
const b = new URL('http://foo');
|
||||
a.bar = 1;
|
||||
b.bar = 1;
|
||||
assertDeepAndStrictEqual(a, b);
|
||||
}
|
||||
|
||||
{
|
||||
const a = new URL('http://foo');
|
||||
const b = new URL('http://bar');
|
||||
assert.throws(
|
||||
() => assert.deepStrictEqual(a, b),
|
||||
{
|
||||
code: 'ERR_ASSERTION',
|
||||
name: 'AssertionError',
|
||||
message: /http:\/\/bar/
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user