Last active
December 27, 2015 23:39
-
-
Save bguiz/7407048 to your computer and use it in GitHub Desktop.
When running unit tests using sinon-qunit, it can be rather annoying if you have to click through modal alert dialogs, and console errors can create clutter. However, failure scenarios in which these typically occur still need to be tested. Use this function to satisfy both requirements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Use this method when we want to silence window alerts and console output within | |
* a test case. | |
* Most useful when testing error messages. | |
* | |
* Sample usage: | |
test("should not show any dialogs or output to console", function() { | |
this.silenceAlertsAndConsole(); | |
window.alert("Hello"); | |
console.log("World"); | |
this.silenceAlertsAndConsole(false); | |
}); | |
* | |
* params: | |
* - isSilent: default=true | |
*/ | |
function silenceAlertsAndConsole(isSilent) { | |
if (!! isSilent || _.isUndefined(isSilent)) { | |
//stub results not important, done here just to silence | |
this.sandbox.stub(window, 'alert'); | |
this.sandbox.stub(window.console, 'error'); | |
this.sandbox.stub(window.console, 'log'); | |
} | |
else { | |
window.alert.restore(); | |
window.console.error.restore(); | |
window.console.log.restore(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment