ENV*_*ENV 0 mocha.js node.js typescript
如果这是一个天真的问题,我深表歉意。我对 Typescript 和 Mocha 测试比较陌生。我想测试以下流程以提高代码覆盖率:
process.on('unhandledRejection', (reason, p) => {
LoggingService.error(reason, 'unhandledRejection', 'Unhandled Rejection at:');
});
process.on('uncaughtException', (error) => {
LoggingService.error(error, 'unhandledRejection', 'Caught exception: ');
});
process.on('warning', (warning) => {
LoggingService.info('Warning, Message: ' + warning.message + ' Stack: ' + warning.stack, 'warning');
});
process.on('exit', (code) => {
LoggingService.error(null, 'NodeJs Exit', `Node.js process is about to exit with code: ${code}`);
});
Run Code Online (Sandbox Code Playgroud)
如何在 mocha 中为流程编写测试?特别是,如果我想模拟我process.on如何unhandledRejection, uncaughtException, warning, exit能够做到这一点,以及由于没有return这些过程的声明,我会期待什么?
提前致谢。
您可以在对象上发出事件process来测试事件处理程序。您可以使用sinonLoggingService等库来监视对象,以检查是否使用预期参数调用了方法。
这个console.log基于的例子展示了这个想法:
const sinon = require('sinon');
// This event handler is what we are going to test
process.on('warning', () => {
console.log('received warning');
});
describe('process', () => {
it('should handle "warning" event', (done) => {
const spy = sinon.spy(console, 'log');
process.on('warning', () => {
sinon.assert.calledWith(spy, 'received warning')
done()
});
process.emit('warning')
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1006 次 |
| 最近记录: |