<p dir="auto">Basically, every time <code class="notranslate">setState</code> is called and a rerender happens, I get this error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Invariant Violation: dangerouslyRenderMarkup(...): Cannot render markup in a worker thread. 
Make sure `window` and `document` are available globally before requiring React 
when unit testing or use React.renderToString for server rendering."><pre class="notranslate"><code class="notranslate">Invariant Violation: dangerouslyRenderMarkup(...): Cannot render markup in a worker thread. 
Make sure `window` and `document` are available globally before requiring React 
when unit testing or use React.renderToString for server rendering.
</code></pre></div>
<p dir="auto">I've created a very bare repo where you can recreate the issue:<br>
<a href="https://github.com/dmatteo/setStateBoom">https://github.com/dmatteo/setStateBoom</a></p>
<p dir="auto">The component + test suite is so small that I can copy/paste it here:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
import React, {Component} from 'react/addons';
import jsdomify from 'jsdomify';
import expect from 'unexpected';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.displayName = 'MyComponent';
    this.state = {
      bananas: 'bananas'
    };
  }

  render() {
    return &lt;div&gt;{this.state.bananas}&lt;/div&gt;
  }
}

describe('setState test', () =&gt; {

  before(() =&gt; {
    // this leaks document and window to global
    jsdomify.create();
  });

  it('should render', () =&gt; {
    let instance = React.addons.TestUtils.renderIntoDocument(&lt;MyComponent&gt;&lt;/MyComponent&gt;);
    expect(instance, 'to be defined');
  });

  it('should not throw', () =&gt; {
    let instance = React.addons.TestUtils.renderIntoDocument(&lt;MyComponent&gt;&lt;/MyComponent&gt;);
    let foo = () =&gt; {
      instance.setState({bananas: 'apples'});
    };
    expect(foo, 'not to throw');
  })

});"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-v">React</span><span class="pl-kos">,</span> <span class="pl-kos">{</span><span class="pl-v">Component</span><span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'react/addons'</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-s1">jsdomify</span> <span class="pl-k">from</span> <span class="pl-s">'jsdomify'</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-s1">expect</span> <span class="pl-k">from</span> <span class="pl-s">'unexpected'</span><span class="pl-kos">;</span>

<span class="pl-k">class</span> <span class="pl-v">MyComponent</span> <span class="pl-k">extends</span> <span class="pl-v">Component</span> <span class="pl-kos">{</span>
  <span class="pl-en">constructor</span><span class="pl-kos">(</span><span class="pl-s1">props</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-smi">super</span><span class="pl-kos">(</span><span class="pl-s1">props</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">displayName</span> <span class="pl-c1">=</span> <span class="pl-s">'MyComponent'</span><span class="pl-kos">;</span>
    <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">state</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span>
      <span class="pl-c1">bananas</span>: <span class="pl-s">'bananas'</span>
    <span class="pl-kos">}</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span>

  <span class="pl-en">render</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">return</span> <span class="pl-c1">&lt;</span><span class="pl-ent">div</span><span class="pl-c1">&gt;</span><span class="pl-kos">{</span><span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">state</span><span class="pl-kos">.</span><span class="pl-c1">bananas</span><span class="pl-kos">}</span><span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-ent">div</span><span class="pl-c1">&gt;</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-en">describe</span><span class="pl-kos">(</span><span class="pl-s">'setState test'</span><span class="pl-kos">,</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>

  <span class="pl-en">before</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-c">// this leaks document and window to global</span>
    <span class="pl-s1">jsdomify</span><span class="pl-kos">.</span><span class="pl-en">create</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-en">it</span><span class="pl-kos">(</span><span class="pl-s">'should render'</span><span class="pl-kos">,</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">let</span> <span class="pl-s1">instance</span> <span class="pl-c1">=</span> <span class="pl-v">React</span><span class="pl-kos">.</span><span class="pl-c1">addons</span><span class="pl-kos">.</span><span class="pl-c1">TestUtils</span><span class="pl-kos">.</span><span class="pl-en">renderIntoDocument</span><span class="pl-kos">(</span><span class="pl-c1">&lt;</span><span class="pl-ent">MyComponent</span><span class="pl-c1">&gt;</span><span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-ent">MyComponent</span><span class="pl-c1">&gt;</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-en">expect</span><span class="pl-kos">(</span><span class="pl-s1">instance</span><span class="pl-kos">,</span> <span class="pl-s">'to be defined'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-en">it</span><span class="pl-kos">(</span><span class="pl-s">'should not throw'</span><span class="pl-kos">,</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">let</span> <span class="pl-s1">instance</span> <span class="pl-c1">=</span> <span class="pl-v">React</span><span class="pl-kos">.</span><span class="pl-c1">addons</span><span class="pl-kos">.</span><span class="pl-c1">TestUtils</span><span class="pl-kos">.</span><span class="pl-en">renderIntoDocument</span><span class="pl-kos">(</span><span class="pl-c1">&lt;</span><span class="pl-ent">MyComponent</span><span class="pl-c1">&gt;</span><span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-ent">MyComponent</span><span class="pl-c1">&gt;</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">let</span> <span class="pl-en">foo</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
      <span class="pl-s1">instance</span><span class="pl-kos">.</span><span class="pl-en">setState</span><span class="pl-kos">(</span><span class="pl-kos">{</span><span class="pl-c1">bananas</span>: <span class="pl-s">'apples'</span><span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">;</span>
    <span class="pl-en">expect</span><span class="pl-kos">(</span><span class="pl-en">foo</span><span class="pl-kos">,</span> <span class="pl-s">'not to throw'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span>

<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Do you have any idea what is happening and what can I do about it? (if anything)</p>
<p dir="auto">Currently React relies on a global <code class="notranslate">window</code> (and <code class="notranslate">document</code> and <code class="notranslate">navigator</code>).<br>
This works in a browser and it works in other environments if one sets a global <code class="notranslate">window</code>.<br>
However, relying on globals prevents multiple independent React engines from running together.</p>
<p dir="auto">Testing, for example, benefits strongly from isolated environments. It would be nice if one could run separate tests with <code class="notranslate">jsdom</code> without manually cleaning up the <code class="notranslate">window</code> every time.<br>
Another use case is for programatic behaviour inspection. It's interesting to load multiple windows with <code class="notranslate">jsdom</code> and compare the behavior of one's library with different calls. If React is involved, however, this is impossible.</p>
<p dir="auto">Avoiding relying on globals can be added in a backwards-compatible fashion. If there is a global <code class="notranslate">window</code> and <code class="notranslate">document</code>, keep the existing behavior. If there isn't, instead of exposing an API, expose a factory taking a <code class="notranslate">window</code> and returning an API. For example, see how <a href="https://github.com/jquery/jquery/blob/master/src/intro.js#L25">jQuery did it</a>.</p>
<p dir="auto">A decision to avoid globals means refactoring existing code base to pass <code class="notranslate">window</code> explicitly everywhere, and maintaining that attitude in the future. What is your opinion on such a feature?</p>