title: Mocking `document` in jest
body: <p>I'm trying to write tests for my web components projects in jest. I already use babel with es2015 preset. I'm facing an issue while loading the js file. I have followed a piece of code where <code>document</code> object has a <code>currentScript</code> object. But in test context it is <code>null</code>. So I was thinking of mocking same. But <code>jest.fn()</code> is not really help in same. How can I handle this issue?</p>
  
  <p>Piece of code where jest is failing.</p>
  
  <pre><code>var currentScriptElement = document._currentScript || document.currentScript;
  var importDoc = currentScriptElement.ownerDocument;
  </code></pre>
  
  <p>Test case I have written. <code>component.test.js</code></p>
  
  <pre><code>import * as Component from './sample-component.js';
  
  describe('component test', function() {
    it('check instance', function() {
      console.log(Component);
      expect(Component).toBeDefined();
    });
  });
  </code></pre>
  
  <p>Following is the error thrown by jest</p>
  
  <pre><code>Test suite failed to run
  
      TypeError: Cannot read property 'ownerDocument' of null
  
        at src/components/sample-component/sample-component.js:4:39
  </code></pre>
  
  <p><strong>Update:</strong>
  As per suggestion from Andreas Köberle, I have added some global vars and tried  to mock like following</p>
  
  <pre><code>__DEV__.document.currentScript = document._currentScript = {
    ownerDocument: ''
  };
  __DEV__.window = {
    document: __DEV__.document
  }
  __DEV__.document.registerElement = jest.fn();
  
  import * as Component from './arc-sample-component.js';
  
  describe('component test', function() {
    it('check instance', function() {
      console.log(Component);
      expect(Component).toBeDefined();
    });
  });
  </code></pre>
  
  <p>But no luck</p>
  
  <p><strong>Update:</strong> I have tried above code without <code>__dev__</code>. Also by setting document as global.</p>