在Mocha中测试NodeJS时,域无法正确捕获错误 [英] Domains not properly catching errors while testing nodeJS in mocha

查看:75
本文介绍了在Mocha中测试NodeJS时,域无法正确捕获错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行使用域进行错误处理的测试时,即使库中的域处理程序应该已经捕获了该错误,Mocha仍然似乎抛出错误.如果我在Mocha之外执行代码,那么它可以正常工作,使我认为问题出在Mocha.

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.

示例:

foo.js

module.exports = function(done) {
    var domain = require("domain");
    var d = domain.create();

    d.on("error", function() {
        done();
    });

    d.run(function() {
        throw new Error("foo");
    });
}

test.js -域未捕获foo.js内部引发的错误.

test.js - Error thrown inside foo.js is not being caught by the domain.

describe("test", function() {
    it("should succeed", function(done) {
        var foo = require("./foo.js");
        foo(function() {
            console.log("done");
            done();
        });
    });
});

result : error thrown

script.js -域已正确捕获错误并将其冒泡.

script.js - error is being properly caught by the domain and bubbled up.

var foo = require("./foo.js");
foo(function() {
    console.log("done");
});
result : done

如您在上面看到的,如果我直接连接到script.js,它会按需运行,那么该错误将被域处理程序捕获,并且代码将继续.如果我在Mocha测试中运行相同的代码块,该错误将终止测试并给出失败.我相信这是因为错误是在uncaughtException处理程序或类似的东西上发送的.另一个复杂之处在于,如果我在函数调用周围有一个process.nextTick(),它可以在Mocha中正常工作,这使我相信Mocha只能处理同步错误,但对于异步错误也可以正常工作.

As you can see above, if I node straight to script.js 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.

这里有一些有关此问题的讨论: https://groups.google.com/forum/#!msg/nodejs/n-W9BSfxCjI/SElI1DJ_6u0J

There is some talk of this problem here: https://groups.google.com/forum/#!msg/nodejs/n-W9BSfxCjI/SElI1DJ_6u0J and https://github.com/joyent/node/issues/4375 .

我的困惑是,所有这些讨论似乎都表明问题已经在几个月前解决了.任何人都知道该问题的简单解决方法,或者是为什么我没有看到一个错误修复程序,而这个错误似乎已经被其他人认为已经解决.

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.

我正在Windows 7的CentOS 6.3 Vagrant VirtualBox上运行节点v0.10.18和Mocha 1.13.0.

I am running node v0.10.18 and Mocha 1.13.0 on CentOS 6.3 Vagrant VirtualBox on Windows 7.

推荐答案

发现了问题. NodeJS域捕获同步错误,但事件继续冒泡到try/catch .如果将domain.run()包裹在try/catch中,则将执行域错误处理程序和catch.

Found the problem. NodeJS domains catch synchronous errors but the event continues to bubble to a try/catch. If you wrap a domain.run() in a try/catch then the domain error handler AND the catch will be executed.

因此,看来最佳实践是在所有domain.run()中使用process.nextTick.这在docs示例中已显示,但未如我希望的那样明确表示.

Thus, it seems the best practice is to use process.nextTick inside all domain.run(). This is shown in the docs example, but isn't expressed as explicitly as I would prefer.

示例:

d.run(function() {
    process.nextTick(function() {
        // do stuff
    });
});

在这种情况下,该缺陷不在Mocha中.

In this case, the flaw is not in Mocha.

NodeJS域的证明未在try/catch中捕获同步错误: https://gist.github.com/owenallenaz/7141699

Proof of NodeJS domains not catching synchronous errors in try/catch: https://gist.github.com/owenallenaz/7141699

这篇关于在Mocha中测试NodeJS时,域无法正确捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆