如何在摩卡中测试未捕获的错误?

时间:2012-01-26 20:47:36

标签: javascript unit-testing exception node.js mocha

我想测试以下函数是否按预期执行:

function throwNextTick(error) {
    process.nextTick(function () {
        throw error;
    });
}

这是我的尝试:

describe("throwNextTick", function () {
    it("works as expected", function (next) {
        var error = new Error("boo!");
        var recordedError = null;
        process.once("uncaughtException", function (error) {
            recordedError = error;
        });

        throwNextTick(error);

        process.nextTick(function () {
            recordedError.should.be(error);
            next();
        });
    });
});

但是mocha似乎想要保留任何错误,并且当它得到它们时我的测试失败了:

C:\Users\ddenicola\Programming (Synced)\pubit>mocha test/basicTest.js

  throwNextTick
    0) works as expected

  ? 1 of 1 tests failed:

  1) throwNextTick works as expected:
     Error: boo!
      at Test.fn (C:\Users\ddenicola\Programming (Synced)\pubit\test\basicTest.js:11:21)
      at Test.run (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:144:15)
      at Runner.runTest (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:271:10)
      at C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:315:12
      at next (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:199:14)
      at C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:208:7
      at next (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:157:23)
      at Array.0 (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:176:5)
      at EventEmitter._tickCallback (node.js:192:40)

有什么想法吗?

3 个答案:

答案 0 :(得分:29)

更新:在下面的评论中提供关于casey-foster的信息:

  

从节点v6.0.0开始,您可以使用process.prependOnceListener('uncaughtException', ...)更简洁地执行此操作。


旧答案:

秘密在于process.listeners('uncaughtException'):

http://nodejs.org/docs/latest/api/events.html#emitter.listeners

只需删除mocha侦听器,添加自己的侦听器,然后重新附加mocha侦听器。

见下文:

var assert = require('assert')

function throwNextTick(error) {
    process.nextTick(function () {
        throw error
    })
}


describe("throwNextTick", function () {
    it("works as expected", function (next) {
        var error = new Error("boo!")
        var recordedError = null
        var originalException = process.listeners('uncaughtException').pop()
        //Needed in node 0.10.5+
        process.removeListener('uncaughtException', originalException);
        process.once("uncaughtException", function (error) {
            recordedError = error
        })
        throwNextTick(error);
        process.nextTick(function () {
            process.listeners('uncaughtException').push(originalException)
            assert.equal(recordedError, error)
            next()
        })
    })
})

答案 1 :(得分:0)

如果您的异步代码在域中执行 - 而且通常就是这种情况 - 您需要更改域上的错误侦听器而不是进程。

为此您可以使用:

it('should produce an unhandled exception', function (done) {

    // Remove Mocha's error listener
    var originalErrorListeners = process.domain.listeners('error');
    process.domain.removeAllListeners('error');

    // Add your own error listener to check for unhandled exceptions
    process.domain.on('error', function () {

        // Add the original error listeners again
        process.domain.removeAllListeners('error');
        for ( var i = 0; i < originalErrorListeners.length; i+=1 ) {
            process.domain.on('error', originalErrorListeners[i]);
        }

        // For the sake of simplicity we are done after catching the unhandled exception
        done();

    });

    // This would be your async application code you expect to throw an exception
    setTimeout(function () {
        throw new Error();
    });

});

答案 2 :(得分:-1)

基于timoxley&amp; Casey Foster,节点v6 ++

const assert = require('assert')

describe('throwNextTick', function() {
    it('works as expected', function(next) {

        function cb(err) {
            assert.equal(err instanceof Error, true)
            next()
        }

        function test(){
            process.nextTick(() => {
                throw new Error('err')
            })
        }

        process.prependOnceListener('uncaughtException', cb)
        test()

    })
})