title: Domains not properly catching errors while testing nodeJS in mocha
body: <p>When running tests that utilize domains for error handling, Mocha still appears to be throwing an error even if a domain handler inside a library should have caught the error. If I execute the code outside of Mocha, it functions correctly leading me to believe the problem is Mocha.</p>
  
  <p>Example:</p>
  
  <p><strong>foo.js</strong></p>
  
  <pre><code>module.exports = function(done) {
      var domain = require("domain");
      var d = domain.create();
  
      d.on("error", function() {
          done();
      });
  
      d.run(function() {
          throw new Error("foo");
      });
  }
  </code></pre>
  
  <p><strong>test.js</strong> - Error thrown inside <code>foo.js</code> is not being caught by the domain.</p>
  
  <pre><code>describe("test", function() {
      it("should succeed", function(done) {
          var foo = require("./foo.js");
          foo(function() {
              console.log("done");
              done();
          });
      });
  });
  
  result : error thrown
  </code></pre>
  
  <p><strong>script.js</strong> - error is being properly caught by the domain and bubbled up.</p>
  
  <pre><code>var foo = require("./foo.js");
  foo(function() {
      console.log("done");
  });
  result : done
  </code></pre>
  
  <p>As you can see above, if I node straight to <code>script.js</code> it functions as desired, the error is caught by the domain handler and the code continues. If I run the same block of code inside a Mocha test, the error halts the test and a failure is given. I believe this is because the error is being sent on an uncaughtException handler, or something like it. The other complication is that it works properly in Mocha, if I have a process.nextTick() around the function call, leading me to believe that Mocha is only unable to handle synchronous errors, but works just fine with async errors.</p>
  
  <p>There is some talk of this problem here: <a href="https://groups.google.com/forum/#!msg/nodejs/n-W9BSfxCjI/SElI1DJ_6u0J" rel="nofollow">https://groups.google.com/forum/#!msg/nodejs/n-W9BSfxCjI/SElI1DJ_6u0J</a> and <a href="https://github.com/joyent/node/issues/4375" rel="nofollow">https://github.com/joyent/node/issues/4375</a> .</p>
  
  <p>The confusion I have is that all of this discussion seems to state the problem has been resolved months ago. Anyone know of either a simple work-around for the issue, or a why I'm not seeing a bug fixed which other people seem to believe is fixed at this point in time.</p>
  
  <p>I am running node v0.10.18 and Mocha 1.13.0 on CentOS 6.3 Vagrant VirtualBox on Windows 7.</p>