禁止成功进行Mocha测试的控制台日志 [英] Suppress console log of successful mocha tests

查看:117
本文介绍了禁止成功进行Mocha测试的控制台日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行mocha测试套件时,我的控制台输出被应用程序日志污染了.有没有一种简便的方法可以抑制成功测试中的这些日志?我可以禁止测试环境中的所有日志,但是我想查看失败测试中的日志.我正在使用温斯顿作为日志记录库.

When running mocha test suite, my console output is polluted with application logs. Is there an easy way to suppress these logs from successful tests? I'm able to suppress all logs for testing environment, however I'd like to see logs from failed tests. I'm using Winston as logging library.

推荐答案

您可以在运行测试时设置单独的Winston传输方式,该测试会将日志写入日志文件,并在测试失败时显示该文件的内容:

You could set up a separate Winston transport when running tests that will write logs to a logfile, and show the contents of that file if the test failed:

afterEach(function() {
  if (this.currentTest.state !== 'failed') return;
  console.log( fs.readFileSync('/your/logfile').toString() );
});

在每次测试之前,您都将文件删除以重新开始:

Before each test you'd remove the file to start fresh:

beforeEach(function(done) {
  try { fs.unlinkSync('/your/logfile'); } catch(e) {}
});

这篇关于禁止成功进行Mocha测试的控制台日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆