1

I have a util that I want to test:

var util = function(data) {
 .....
 console.log('msg');
 ....
 return true
}

My test.js

describe('util', function () {
 it('should ...', function () {
 expect(util({})).to.be.true;
 });
});

How can I avoid 'msg' output during the test without changing the util?

Alexey B.
12k2 gold badges51 silver badges76 bronze badges
asked Nov 26, 2014 at 12:10
3
  • I would use github.com/flatiron/winston logging framework to do so. By which I can log with preferred log level. Commented Nov 26, 2014 at 12:15
  • Why not executing "console.log = function () {};" before your test starts ? It's a quick and dirty solution/workaround though, solutions like winston should be more suitable. Commented Nov 26, 2014 at 12:25
  • 1
    it also suppress mocha reporter logs Commented Nov 26, 2014 at 12:26

2 Answers 2

1

I don't know much about mocha but would this work?

describe('util', function () {
 it('should ...', function () {
 var logg = console.log;
 console.log = function(m){};
 expect(util({})).to.be.true;
 console.log = logg;
 });
});
answered Nov 26, 2014 at 12:35
Sign up to request clarification or add additional context in comments.

1 Comment

I prefer console = {log: function(){}, info: ...} :)
1

Preferably you would use something like winston, otherwise you will have to use conditionals.

I have a small utility for that, first I will show how to use it then I will show the code;

ifEnvironmentIs('production').log('msg'); //Prints message only in production
ifEnvironmentIs('test').or('staging').log('msg'); //only in test and staging

And the code is like this

function environmentIs(desired) {
 if(!Array.isArray(desired)) {
 desired = [desired];
 }
 return desired.indexOf(NODE_ENV) > -1;
}
function ifEnvironmentIs(desired) {
 desired = [desired];
 var that = {};
 that.then = function(fn) {
 if(environmentIs(desired)) {
 fn();
 }
 return that;
 };
 that.or = function(env) {
 desired.push(env);
 return that;
 },
 that.otherwise = function(fn){
 if(!environmentIs(desired)) {
 fn();
 }
 return that;
 };
 that.log = function(text) {
 if(environmentIs(desired)) {
 console.log(text);
 }
 return that;
 };
 return that;
}

It works based on environment variables, which is the best way to differentiate between your running environments, like TEST, PRODUCTION and DEVELOPMENT

You can use the code provided also to execute function conditionally by using the .then and .otherwise functions.

You can extend this code to include your custom logging method (instead of console)

answered Nov 26, 2014 at 12:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.