diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 622dab4eae8a..3d36c5793bea 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -205,7 +205,7 @@ function pathToFileUrlHref(filepath) { function isURL(value) { internalUrl ??= require('internal/url'); - return typeof value.href === 'string' && value instanceof internalUrl.URL; + return internalUrl.isURLInstance(value) && typeof value.href === 'string'; } const builtInObjects = new SafeSet( diff --git a/test/parallel/test-util-inspect-url.js b/test/parallel/test-util-inspect-url.js new file mode 100644 index 000000000000..3635e9ab9f75 --- /dev/null +++ b/test/parallel/test-util-inspect-url.js @@ -0,0 +1,35 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const { format, inspect } = require('util'); + +class TestURL extends URL {} + +assert.strictEqual(inspect(TestURL.prototype), 'URL {}'); +assert.strictEqual( + inspect(TestURL.prototype, { customInspect: false }), + 'URL {}', +); + +// Inspecting hidden properties must not throw when reaching the prototype. +inspect(TestURL, { showHidden: true }); +inspect({ TestURL }, { showHidden: true }); +format('%o', { TestURL }); + +const url = new TestURL('https://example.org/'); +assert.strictEqual( + inspect(url, { customInspect: false }), + 'https://example.org/', +); + +class NonStringURL extends URL { + get href() { + return undefined; + } +} + +assert.strictEqual( + inspect(new NonStringURL('https://example.org/'), { customInspect: false }), + 'NonStringURL [URL] {}', +);