title: Testing window.location.origin polyfill
body: <p>I'd like to write a test to prove a polyfill works. I'm using <code>mocha</code>, <code>chai</code>, and <code>jsdom</code>. <code>jsdom</code> exposes <code>window.location.origin</code> as a readonly property so I can't force the polyfill to be used (throws <code>TypeError: Cannot set property origin of [object Object] which has only a getter</code> when attempting to set <code>global.window.location.origin = null</code>;</p>
  
  <p>polyfill:</p>
  
  <pre><code>export default function polyfills () {
      if (typeof window === 'undefined') {
          return;
      }
  
      // http://tosbourn.com/a-fix-for-window-location-origin-in-internet-explorer/
      if (!window.location.origin) {
          let loc = window.location,
              port = loc.port ? `:${loc.port}` : '';
  
          loc.origin = `${loc.protocol}//${loc.hostname}${port}`;
      }
  }
  </code></pre>
  
  <p>jsdom setup:</p>
  
  <pre><code>import jsdom from 'jsdom';
  
  global.document = jsdom.jsdom(
      '&lt;!doctype html&gt;&lt;html&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;',
      {
          url: 'https://www.google.com/#q=testing+polyfills'
      }
  );
  
  global.window = document.defaultView;
  global.navigator = window.navigator;
  </code></pre>
  
  <p>test:</p>
  
  <pre><code>import {expect} from 'chai';
  import polyfills from '../path/to/polyfills';
  
  describe('the polyfill function', function () {
      before(() =&gt; {
          delete global.window.location.origin;
  
          // polyfills(); // test still passes when this is commented out
      });
  
      it('correctly exposes the window.location.origin property', () =&gt; {
          expect(global.window.location.origin).to.equal('https://google.com');
      });
  });
  </code></pre>