scar

test runner for Node.js and the browser

licenseMITgithublrsjng/scarnpmscar
0.19.0

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
  -s: show test stats

Arguments:
  all arguments are used as test filters

API

scar

const obj = require('scar')  // nodejs
var obj = window.scar        // browser

then obj is

{
    Scar,    // constructor
    test,    // preconstructed Scar().static()
    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()
test.scar                     // scar instance

assert

assert(expr, msg)                  // !!expr === true
assert.fail(msg)
assert.ok(act, msg)                // !!act === true
assert.notOk(act, msg)             // !act === true
assert.equal(act, exp, msg)        // act === exp
assert.notEqual(act, ref, msg)     // act !== ref
assert.deepEqual(act, exp, msg)    // act === exp for all but Array and plain Object
assert.notDeepEqual(act, ref, msg) // !deepEqual()
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.isId(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 aSpyFn = spy(() => 42);
aSpyFn.calls         // => []
aSpyFn(11, 22, 33)   // => 42
aSpyFn.calls[0]      // => {idx: 0, ctx: undefined, args: [11, 22, 33], ret: 42, time: ..., done: ...}
Works best with JavaScript enabled!Works best in modern browsers!