如何测试process.on是否有未处理的拒绝

我了解我如何可以使用诺言进入不处理拒绝类型。 在我的index.js中,我有以下几行:

process.on('unhandledRejection',(result,error) => {
process.exit(1);
}

我需要在单元测试之前进行覆盖,我该怎么做?

我的测试:

describe('Test unhandledRejection',() => {
    it('unhandledRejection ',function runner(done) {
        try {
            const p = Promise.resolve("resolved");
            assert.isRejected(p);
        } catch (error) {
            console.log('unhandledRejection');
        }

        done();
    });
});

caosh666666 回答:如何测试process.on是否有未处理的拒绝

您可以使用以下应该抛出但通过测试的测试:

     it('unhandledRejection ',function runner(done) {
        process.on('unhandledRejection',(reason,promise) => {
            done();
        });

        async function main () {
            try {
                await doesntexist; // . will throw
            }
            catch(err) {
                console.error(err);
                throw err;
            }
        }

        main();
    });
本文链接:https://www.f2er.com/3107647.html

大家都在问