scar
test runner for Node.js and the browser
Test runner for Node.js and the browser, extensively tested with mocha and itself. Basic assertion functions are included, but it should work fine with any assertion lib that throws and rejects. Supports async tests through Promise
. Lightweight object inspection, unique IDs and function spies included.
Examples
Works fine with Node.js, Webpack+Babel and Browserify+Babel.
const {test, assert} = require('scar');
test('passing', () => {
assert.equal(1, 1);
});
test('failing', () => {
assert.equal(1, 2);
});
test.cli();
Might need additional polyfills for Promise
, Object.assign
and Array.prototype.every
to work in older browsers.
<script src="scar.js"></script>
<script>
var test = window.scar.test;
var assert = window.scar.assert;
test('passing', function () {
assert.equal(1, 1);
});
test('failing', function () {
assert.equal(1, 2);
});
test.cli();
</script>
CLI
scar - a test runner for node and the browser
Usage:
node your-script.js [opt...] [arg...]
your-url.html?opt&...&arg&...
Options:
-h: show this help message
Arguments:
all arguments are used as test filters
API
scar
const obj = require('scar') // nodejs
var obj = window.scar // browser
then obj
is
{
scar, // factory
test, // preconstructed scar()
assert, // lightweight assertions
insp, // lightweight value inspection
uniq, // unique IDs and objects
spy // lightweight function spies
}
test
test(desc, fn, {skip, sync}) // args optional and in any order
test.skip(desc, fn, {sync}) // like {skip: true}
test.sync(desc, fn, {skip}) // like {sync: true}
test.run() // run all tests => Promise()
test.cli() // run CLI => Promise()
assert
assert(expr, msg) // !!expr === true
assert.fail(msg)
assert.ok(act, msg) // !!act === true
assert.not_ok(act, msg) // !act === true
assert.equal(act, exp, msg) // act === exp
assert.not_equal(act, ref, msg) // act !== ref
assert.deep_equal(act, exp, msg) // act === exp for all but Array and plain Object
assert.not_deep_equal(act, ref, msg) // !deep_equal()
assert.throws(fn, exp, msg)
assert.rejects(thenable, exp, msg) // => Promise
insp
insp(x) // => string repr of x
uniq
uniq.id() // => 'UNIQ-0007-ID'
uniq.is_id(x) // => x matches 'UNIQ-xxxx-ID'
uniq.obj() // => {_uniq_id: 'UNIQ-0013-ID'}
uniq.path(ext) // => '_uniq_path/UNIQ-0021-ID.ext'
spy
spy([fn]) // => spy function, with optional call handler 'fn(call, calls)'
Example usage:
const a_spy_fn = spy(() => 42);
a_spy_fn.calls // => []
a_spy_fn(11, 22, 33) // => 42
a_spy_fn.calls[0] // => {idx: 0, ctx: undefined, args: [11, 22, 33], ret: 42, time: ..., done: ...}