Detecting console.log() calls-Collection of common programming errors
You can replace console.log
with your own function, which determines if it’s been called correctly. Here’s a example of how you could confirm that a function logs a certain message:
function doesLogMessage(f, message) {
var oldLog = console.log,
result = false;
console.log = function(s) {
if (s == message) {
result = true;
}
};
f();
console.log = oldLog;
return result;
}
Example Usage
function exampleFunction() {
console.log("Hello World");
}
doesLogMessage(exampleFunction, "Hello World"); // true
function exampleFunction2() {
console.log("Preparing world...");
console.log("Hello World");
}
doesLogMessage(exampleFunction2, "Hello World"); // true
function exampleFunction3() { }
doesLogMessage(exampleFunction3, "Hello World"); // false
function exampleFunction4() {
console.log("Goodbye World");
}
doesLogMessage(exampleFunction4, "Hello World"); // false