How to suppress application logging messages from a node.js application when running unit tests?

In your app.js:

if (process.env.NODE_ENV !== 'test') {
  app.use(express.logger());
}

At the top of each of your mocha files:

process.env.NODE_ENV = 'test';

Update:

We use this function in our import code:

function logExceptOnTest(string) {
  if (process.env.NODE_ENV !== 'test') {
    console.log(string);
  }
}

Then, replace all your console.log('it worked') with logExceptOnTest('it worked'). The basic trick is to use environment variables as a global flag as to the level of logging you want.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)