Skip to content

Instantly share code, notes, and snippets.

@bguiz
Last active December 27, 2015 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bguiz/7407048 to your computer and use it in GitHub Desktop.
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.
/*
* 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