Create a gist now

Instantly share code, notes, and snippets.

A visual and console implementation of a basic assert in JavaScript - assertions with a assertion panel in your browser window without intruding upon your other content.
by keith rosenberg / netpoetica
An assert function that will create a visual pane if browser is present. It will always
output to console as well. The styling is configured to be position-fixed to the top
right corner (hopefully in most situations, out of the way). I created this after viewing
How to Use
In your own code, after this script has been loaded, you simply call
assert(true == false, 'True equals false');
assert(4 === '4', '4 is equal to "4");
assert(0 == false, '0 is false');
The first argument is your test (must return a boolean variable), and the second argument
is the description it will spit back at you to let you know which assertion to troubleshoot.
Where is the JS file located?
Copy and Paste the following code to dynamically load this script:
// Dynamically load scripts
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '';
var assert = (function(){
// We're in a browser, so set up some extras for viewing.
var bBrowserEnv = true;
// Setup the module container
var assertViewer = document.createElement('div'); = '#EEF'; = '9999'; = 'fixed'; = 'auto'; = '25px'; = '25px';
// Add a header
var assertViewerHeader = document.createElement('h4');
assertViewerHeader.appendChild(document.createTextNode('Assert Panel')); = '25px';
// Setup the assertions container
var assertContainer = document.createElement('ul');
// Self Titled Function
function addAssertItem(sDesc, bPass){
var li = document.createElement('li');
bPass ? = '#0000AF' : = '#FA0000';
return li;
return function(bExpected, sDescription){
var output = '';
output += 'PASS: ';
else {
output += 'FAIL: ';
output += String(sDescription);
if(bBrowserEnv){ assertContainer.appendChild(addAssertItem(output, bExpected)) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment