Skip to content

Instantly share code, notes, and snippets.

@jaichandra
Forked from jeromeetienne/dieassert.js
Created November 15, 2020 15:56
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 jaichandra/ab7fa9d9b894ad19a0b9bd872e36c5fe to your computer and use it in GitHub Desktop.
Save jaichandra/ab7fa9d9b894ad19a0b9bd872e36c5fe 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!");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment