Skip to content

Instantly share code, notes, and snippets.

@jaeschrich
Last active July 20, 2022 20:49
Show Gist options
  • Save jaeschrich/4518368 to your computer and use it in GitHub Desktop.
Save jaeschrich/4518368 to your computer and use it in GitHub Desktop.
A tiny node.js test framework with no dependencies
/*
Example
var test = require('./path/to/test'),
cake = require('..'),
assert = require("assert");
test('eat cake', function(done){
cake.eat();
done();
});
test('something that will throw an error', function(done){
assert.equal('apples', 'oranges');
done();
});
test.runTests();
*/
var domain = require("domain"),
green = "\x1b[32;1m",
cancel = "\x1b[0m",
yellow = "\x1b[33;1m",
grey = "\x1b[37;0m",
red = "\x1b[31;1m";
function done(text){
console.log(green+"done"+cancel+": "+text);
return true;
}
function fail(text, err){
console.log(red+"fail"+cancel+": "+grey+text+"\n\t"+yellow+err.toString()+cancel);
}
function test(name, fn){
test._arr.push({ name: name, fn: fn });
}
test.fail = fail;
test.done = done;
test._arr = [];
test.runTests = function () {
var arr = this._arr;
(function runNext(c) {
var nextIndex = arr.indexOf(c) + 1;
if (nextIndex === 0) process.exit(0);
var cur = c.fn;
var name = c.name;
var next = arr[nextIndex];
function ldone() {
done(name);
runNext(next);
}
var d = domain.create();
d.on('error', function (err) {
fail(name, err);
runNext(next);
});
d.run(function () {
cur(ldone);
});
})(arr[0]);
};
module.exports = test;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment