Suppress console log of successful mocha tests

2019-07-12 01: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条回答
冷血范
2楼-- · 2019-07-12 01:42

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) {}
});
查看更多
登录 后发表回答