'How to disable Morgan (request logger) during unit test?

I use Morgan (default express generator request logger), and I'm trying to disable it during unit testing.

Currently I'm using the default configuration, which loads Morgan in app.js

const logger = require('morgan');
...
const app = express();
...
app.use(logger('dev'));

I tried moving the code to bin/www (which imports the express app and starts the server), but it wouldn't work... Any ideas?



Solution 1:[1]

You can use skip option of morgan like this:

const logger = require('morgan');
const app = express();
app.use(logger('dev', { skip: (req, res) => process.env.NODE_ENV === 'test' }));

When you run the unit test, the process.env.NODE_ENV will be set to 'test'. See Environment Variables

NODE_ENV - Set to 'test' if it's not already set to something else.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1