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.
Turn Off All Console Information If using Jest test framework, you can turn off all console messages by one command: jest --silent .
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.
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.
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.
Set client.captureConsole = false
in your karma.conf.js
config set function.
module.exports = function (config) {
config.set({
client: {
captureConsole: false
}
});
};
Original feature request.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With