Suppress console log of successful mocha tests

流过昼夜 提交于 2019-12-24 03:33:20

问题


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.


回答1:


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) {}
});


来源:https://stackoverflow.com/questions/31449000/suppress-console-log-of-successful-mocha-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!