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!"); | |
}; |
This comment has been minimized.
This comment has been minimized.
Here's a forked version w/ a third param: https://gist.github.com/2651966 |
This comment has been minimized.
This comment has been minimized.
i coded the modifications you adviced. The third parameter is console.assert.useDebugger in my version. thus it is backward compatible |
This comment has been minimized.
This comment has been minimized.
and you removed the |
This comment has been minimized.
This comment has been minimized.
Ok, now it's time to use it in real life and see if it proves useful :) |
This comment has been minimized.
This comment has been minimized.
This pauses within the stack of the custom 'assert' function, further search I have found that you can pause on |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Sweet! Just two notes:
If it throws, it won't reach the debugger statement, so I'd introduce a third param.
Also, there was some issue with throwing without a
new Error()
... I don't remember exactly what it was, but I think it won't show a stack trace on some browsers or something like that.