Last active
April 12, 2019 17:14
-
-
Save iamjinme/e852ea46f1aa2db6b63fb5b6ed9f6b6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! by @iamjinme, inspired by https://github.com/WebReflection/tressa */ | |
// full module, compatible with node 0.8+ & browser | |
// Add this code in your index.html before your tests | |
// like <script src="minassert.js"></script> | |
// Make your test: assert( 1, 2, "compare two numbers" ); | |
// Look the results in the browser console | |
// for sync tests | |
function assert(actual, expected, message) { | |
try { | |
if (actual !== expected) { | |
throw "Assertion failed: " + message; | |
} else if (typeof message === 'string' && typeof actual !== 'undefined') { | |
console.log('\x1b[32m%s\x1b[0m', '✔ ', message); | |
} | |
} catch(error) { | |
assert.exitCode = 1; | |
console.error('✖ ' + error, { | |
actual, | |
expected, | |
}); | |
} | |
} | |
// for async tests | |
assert.async = function (fn, timeout) { | |
var timer = setTimeout( | |
function () { assert(false, 'timeout ' + fn); }, | |
timeout || assert.timeout | |
); | |
fn(function () { clearTimeout(timer); }); | |
}; | |
// default timeout | |
assert.timeout = 10000; | |
// for node env only | |
try { | |
process.on('exit', function () { | |
process.exit(assert.exitCode || 0); | |
}); | |
module.exports = assert; | |
} catch(browser) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment