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