Mr. Dzhen Mr. Dzhen - 6 months ago 12
Javascript Question

How in Mocha test frunction with console.log statement?

Sorry for my english. Supposing, I have a function:

function consoleOutput(param) {
var newParam = param * param;
console.log(newParam);
}


How can I test in Mocha, that this function will be work correct (param will be multiplied by two and output to the console). Thanks.

Answer

Please take a look on the below code, you will get an idea on mocha test case

var assert = require('chai').assert;

// Function to be Tested
function consoleOutput(param) {
  var newParam = param * param;  
  console.log(newParam);
  return newParam;
}

// Mocha Test Case
describe('Mocha Test Case', function() {
    it('Square Root', function(done) {
        var sqrt = consoleOutput(5);
        assert.equal(sqrt, 25);
        done();
    });
});