title: How to write a test using Mocha+Chai to expect an exception from setTimeout?
body: <p>I have following:</p>
  
  <pre><code>it('invalid use', () =&gt; {
    Matcher(1).case(1, () =&gt; {});
  });
  </code></pre>
  
  <p>The <code>case</code> method is supposed to throw after some delay, how can I describe it for Mocha/Chai that's what I want - the test should pass (and must not pass when exception is not thrown)?</p>
  
  <p><strong>Consider <code>case</code> method off limits, it cannot be changed.</strong></p>
  
  <p>For testing purposes it should be equivalent to:</p>
  
  <pre><code>it('setTimeout throw', _ =&gt; {
    setTimeout(() =&gt; { throw new Error(); }, 1); // this is given, cannot be modified
  });
  </code></pre>
  
  <p>I tried:</p>
  
  <pre><code>it('invalid use', done =&gt; {
    Matcher(1).case(1, () =&gt; {});
    // calls done callback after 'case' may throw
    setTimeout(() =&gt; done(), MatcherConfig.execCheckTimeout + 10);
  });
  </code></pre>
  
  <p>But that's not really helping me, because the test behavior is exactly reverted - when an exception from <code>case</code> (<code>setTimeout</code>) is not thrown, it passes (should fail) and when an exception is thrown the test fails (should succeed).</p>
  
  <p>I read somewhere someone mentioning global error handler, but I would like to solve this cleanly using Mocha and/or Chai, if it is possible (I guess Mocha is already using it in some way).</p>