Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress console logs from code being tested

I'm unit testing a function which contains a console.log() call.

How can I prevent the output of that call from being displayed in the output console of my test runner, in this case, Karma.
I'm using Jasmine.

I'm looking for a more elegant way than overriding browser's console methods, preferably a config.

like image 515
Francisc Avatar asked May 05 '15 12:05

Francisc


People also ask

How do I cover console log in jest?

Turn Off All Console Information If using Jest test framework, you can turn off all console messages by one command: jest --silent .

Does console log slow down code?

Using console. log will use CPU cycles. In computing, nothing is "free." The more you log, the slower your code will execute. This is also true in Apex.

How do I hide errors in console?

console. clear(); is good option to hide the console error because some time on running site we don't want to show error so we hide them in PHP.

How do I remove all console log in production?

if you don't have environment variable then you can jsut simply do. console. log = function () {}; I am using this on my live app to hides the console.


2 Answers

Set client.captureConsole = false in your karma.conf.js config set function.

module.exports = function (config) {
    config.set({
        client: {
            captureConsole: false
        }
    });
};

Original feature request.

like image 163
Glenn Murrary Avatar answered Sep 30 '22 14:09

Glenn Murrary


The problem with the accepted answer is that it also suppress Karma logs.

If you only want to suppress the logging for the called methods set browserConsoleLogOptions.level to an appropriate level in your karma.conf.js. Setting browserConsoleLogOptions.level to "warn" will suppress all the log and debug logs.

copy-paste-ready snippet:

// file: karma.conf.js

module.exports = function (config) {
    config.set({
        // other options...
        browserConsoleLogOptions: {level: "warn"}
    }
}

See the karma configuration file documentation for references.

like image 43
melfnt Avatar answered Sep 30 '22 13:09

melfnt