Skip to content

Instantly share code, notes, and snippets.

@donsalvadori
Created October 2, 2016 20:06
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 donsalvadori/1e94aae6cacc275deeace20940a54114 to your computer and use it in GitHub Desktop.
Save donsalvadori/1e94aae6cacc275deeace20940a54114 to your computer and use it in GitHub Desktop.
(function () {
// The test queue:
var tests = [];
// Function to add tests:
this.test = function test (name, cb) {
tests.push({name: name, test: cb});
};
this.run = function run () {
var i = 0, testToRun;
(function next (err) {
// Log status for last test run:
if (testToRun) console[err ? 'error' : 'log'](err ? '✘' : '✔', testToRun.name);
// Abort if last test failed or out of tests:
if (err || !(testToRun = tests[i++])) return done(err);
// Run test:
try {
testToRun.test.call(testToRun.test, next);
} catch (err) {
next(err);
}
})();
function done (err) {
// Show remaining tests as skipped:
tests.slice(i).map(function (skippedTest) { console.log('-', skippedTest.name); });
// We're finished:
console[err ? 'error' : 'log']('\nTests ' + (err ? 'failed!\n' + err.stack : 'succeeded!'));
}
};
})();
// Example usage:
test('1+1 equals 2', function (done) {
assert(1+1 === 2, '1+1 should be 2');
done();
});
test('1+2 equals 3', function (done) {
assert(1+2 === 3, '1+2 should be 3');
done();
});
// Add a failing test:
test('1+2 equals 4', function (done) {
assert(1+2 === 4, '1+2 should also be 4');
done();
});
// Run all tests:
run();
// assertion fx
function assert (condition, message) {
if (!condition) throw new Error(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment