问题
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