How can I test uncaught errors in mocha?

前端 未结 3 1617
感情败类
感情败类 2021-02-05 11:09

I would like to test that the following function performs as expected:

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


        
3条回答
  •  轮回少年
    2021-02-05 11:55

    Base on timoxley & Casey Foster, in node 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()
    
        })
    })
    

提交回复
热议问题