title: Jest mock window object before importing a dependency
body: <p>I need to have a value set in the window object before a dependency is imported. Say I have this code</p>
  
  <pre class="lang-js prettyprint-override"><code>// foo.test.js
  import { dependency } from './foo'
  
  describe('...', () =&gt; {
    it('...', () =&gt; {
      // use dependency
    })
  })
  </code></pre>
  
  <p>But for dependency to be imported I need to have a value defined in <code>window.myValues</code></p>
  
  <pre class="lang-js prettyprint-override"><code>// foo.js
  export const dependency = {
    key: window.myValue.nestedValue
  }
  </code></pre>
  
  <p>That code will give me an error when importing the file because <code>window.myValue.nestedValue</code> is trying to access the property <code>nestedValue</code> of undefined.</p>
  
  <p>How can I get that done?</p>
  
  <p><strong>Edit</strong>
  Following <a href="https://stackoverflow.com/a/62271843/5452969">christianeide's answer</a> below I get the following error</p>
  
  <pre><code>  ● Test suite failed to run
  
      TypeError: Cannot convert undefined or null to object
  
        2 |   delete global.window.myValue
        3 |   global.window = Object.create(window)
      &gt; 4 |   global.window.myValue = {
          |                 ^
        5 |     nestedValue: 'someValue'
        6 |   }
        7 | }
  
        at module.exports (jest.setup.js:4:17)
        at node_modules/@jest/core/build/runGlobalHook.js:82:17
        at ScriptTransformer.requireAndTranspileModule (node_modules/@jest/transform/build/ScriptTransformer.js:684:24)
        at node_modules/@jest/core/build/runGlobalHook.js:72:27
        at pEachSeries (node_modules/p-each-series/index.js:8:9)
        at async _default (node_modules/@jest/core/build/runGlobalHook.js:58:5)
        at async runJest (node_modules/@jest/core/build/runJest.js:345:5)
  </code></pre>