diff --git a/.gitignore b/.gitignore index a673b10..69d98af 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ node_modules .nyc_output coverage.lcov yarn-error.log +package-lock.json diff --git a/src/url-browser.js b/src/url-browser.js index 2fb48b4..03a9464 100644 --- a/src/url-browser.js +++ b/src/url-browser.js @@ -1,8 +1,6 @@ 'use strict'; -const defaultBase = self.location ? - self.location.protocol + '//' + self.location.host : - ''; +const defaultBase = self.location && self.location.protocol + '//' + self.location.host; const URL = self.URL; class URLWithLegacySupport { diff --git a/test.js b/test.js index b1d2a22..c5f2ef1 100644 --- a/test.js +++ b/test.js @@ -9,6 +9,26 @@ const isBrowser = typeof document === 'object' && document.nodeType === 9; +test('unspecified base should not throw', (t) => { + t.plan(1); + + if (isBrowser) { + t.doesNotThrow(() => new URL('http://localhost')); + } else { + // Hack to force construction of a browser URL in Node to simulate a React Native-like environment where .location does not exist + global.self = { + URL: global.URL, + URLSearchParams: global.URLSearchParams + }; + /* eslint-disable-next-line global-require */ + const { URLWithLegacySupport: URL } = require('./src/url-browser'); + + t.doesNotThrow(() => new URL('http://localhost')); + + delete global.self; + } +}); + test('relative', (t) => { t.plan(1);