How Can I Wrap an Import With JSDom?
<p>I have a codebase that uses the ES2015 module syntax, ie. <code>import foo from 'bar'</code>.  It also has code like this:</p>

<pre><code>// setup.js
var foo = window.foo || '';
</code></pre>

<p>I would like to use <code>jsdom</code> to provide a mock DOM for my test environment, but I can't figure out how to wrap my imported code.  The <code>import</code> statement has to come before any code, so I can't do this:</p>

<pre><code>import jsdom;

jsdom({
    html: '&lt;div&gt;&lt;/div&gt;',
    done: () =&gt; {
        import setup from 'setup';
    }
})
</code></pre>

<p>But if I leave the import at the top, where it belongs, <code>window</code> is referenced before <code>jsdom</code> can get involved:</p>

<pre><code>import foo from 'bar'; // this uses the DOM
import jsdom;

jsdom({
    html: '&lt;div&gt;&lt;/div&gt;',
    done: () =&gt; () =&gt; { console.log('foo'); }
})
</code></pre>

<p>Is there any way to workaround this without changing to AMD or CommonJS?</p>