When unit testing my node.js application (which is basically a REST backend) using mocha and supertest, I only need a test message on the screen, but stdout is also cluttered with application log messages.
I run unit test with
mocha -R spec .
... and get this output (this should not be):
[App] Listening on port 3000 ... [App] Starting app, hooray! Project API GET /projects [App] entering "projects" module ... â should return an array of projects (317ms)
I marked the application log message using [App]. What I really want will be this output from unit test:
Project API GET /projects â should return an array of projects (317ms)
How can I suppress console.log / warn / error output with an application interleaved with Mocha reporter output?
DECISION:
Following the dankohn approach, I ended up solving my problem (using winston for logging):
(in the node "main" server file, server.js :)
if (process.env.NODE_ENV !== 'test') { logger = new (winston.Logger)({ transports: [ new (winston.transports.Console)(), new (winston.transports.File)({ filename: 'foo.log' }) ] }); } else { // while testing, log only to file, leaving stdout free for unit test status messages logger = new (winston.Logger)({ transports: [ new (winston.transports.File)({ filename: 'foo.log' }) ] }); }
... and to set the env variable, each unit test file starts with:
process.env.NODE_ENV = 'test';
andimeier Mar 28 '14 at 10:15 2014-03-28 10:15
source share