Skip to content

Instantly share code, notes, and snippets.

@jeromeetienne
Created May 10, 2012 08:32
Show Gist options
  • Save jeromeetienne/2651899 to your computer and use it in GitHub Desktop.
Save jeromeetienne/2651899 to your computer and use it in GitHub Desktop.
a console.assert which actually stop the execution
/**
* A console.assert which actually stop the exectution.
* default console.assert() is a plain display, such as console.log() or console.error();
* It doesnt stop the execution like assert() is meant to do. This is a little code to
* "workaround this limitation" :) thanks @jensarp
*
* Usage:
* console.assert(foo === bar); // Will throw if not equal
* console.assert(foo === bar, 'Dude, foo does not equal bar'); // Will throw with custom error message
*
* To trigger js debugger on failed assert, do
* console.assert.useDebugger = true;
*/
console.assert = function(cond, text){
if( cond ) return;
if( console.assert.useDebugger ) debugger;
throw new Error(text || "Assertion failed!");
};
@jensarps
Copy link

Here's a forked version w/ a third param: https://gist.github.com/2651966

@jeromeetienne
Copy link
Author

i coded the modifications you adviced. The third parameter is console.assert.useDebugger in my version. thus it is backward compatible

@jeromeetienne
Copy link
Author

and you removed the text which is more eleguant :)

@jensarps
Copy link

Ok, now it's time to use it in real life and see if it proves useful :)

@danielsokolowski
Copy link

This pauses within the stack of the custom 'assert' function, further search I have found that you can pause on console.assert by enabling it during dev in Chrome Devleoper Tools, see http://stackoverflow.com/questions/12163266/break-into-chrome-debugger-when-console-assert-fails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment