How can I prevent Mocha from trapping unhandled exceptions?
<p>I am writing a test suite for a library that performs handling of exceptions that are not handled by a <code>catch</code> statement. The library listens to <code>error</code> events that trickle up to the global scope (e.g. <code>window.addEventListener("error", ...)</code>).</p>

<p>However, if I want to test whether it is <em>really</em> able to detect unhandled exceptions, I can't because Mocha treats such exceptions as test failures. And I cannot use assertions like <code>expect(foo).to.throw</code>, because if I use these, then the exception <em>is caught</em> by <code>expect</code> and is no longer an <em>unhandled exception</em>: it won't trigger the global listener that my library installs and that I want to test. </p>

<p>I've tried <code>allowUncaught</code> but that did not solve the problem. Here is an example test that reproduces the problem:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/&gt;
    &lt;link href="node_modules/mocha/mocha.css" type="text/css" media="screen" rel="stylesheet" /&gt;
    &lt;script src="node_modules/mocha/mocha.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id="mocha"&gt;&lt;/div&gt;
    &lt;script&gt;
      mocha.setup('bdd');
      mocha.allowUncaught();

      it("should be fine", function (done) {
        // Simulate an error thrown by asynchronous code.
        setTimeout(function () {
          throw new Error("error");
        }, 100);

        // My actual error handler is bigger than this. This is just to
        // simulate what my actual test suite does.
        function listener(ev) {
          // Uninstall ourselves.
          window.removeEventListener("error", listener);
          done(); // We detected the error: everything is fine.
        }
        window.addEventListener("error", listener);
      });

      mocha.run();
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>The test above should pass. However, when I run it Mocha reports one failure and one success! How can I get Mocha to ignore the uncaught exception and let my custom code handle it?</p>