Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 957
Dijkie85 Avatar asked Dec 22 '22 19:12

Dijkie85


1 Answers

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.

like image 52
slideshowp2 Avatar answered Dec 26 '22 00:12

slideshowp2