允许chai / mochatesting将错误冒泡到process.on

我正在编写一个捕获顶级未捕获错误的节点模块,并希望为其编写一些testing。 不幸的是,我最喜欢的框架似乎有意识地抛出和捕获未捕获的exception的问题。

如果我抛出exception,那么错误,然后testing失败。

如果我抛出并捕获错误,它不会冒泡process.on('uncaughtException')

代码atm不起作用

it('Catches errors and return the user and line number', function(done) { blame.init(function (res) { console.log('\n\n', res, '\n\n'); expect(true).should.equal(true); done(); }); expect(function () { undefinedFunction(); }).to.throw('undefinedFunction is not defined'); }); 

@Louis感谢您的post。 不完全是修复,但需要使用那里删除和添加事件监听器方法。

最终解决scheme

 function throwNextTick(error) { process.nextTick(function () { // DO NOT MOVE FROM LINE 7 undefinedFunction(); }) } describe("tattletales", function (next) { it("Throw a error and catch it", function (next) { //Removing and saving the default process handler var recordedError = null; var originalException = process.listeners('uncaughtException').pop(); process.removeListener('uncaughtException', originalException); blame.init(function (err, res) { // Removing the process handler my blame added var newException = process.listeners('uncaughtException').pop(); process.removeListener('uncaughtException', newException); // Putting back on mochas process handler process.listeners('uncaughtException').push(originalException); assert.equal(res.error, 'ReferenceError: undefinedFunction is not defined'); next(); }); throwNextTick(); }) })