Skip to content

Instantly share code, notes, and snippets.

@stringparser
Last active August 29, 2015 14:07
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 stringparser/b96966cb26582b3ac6d5 to your computer and use it in GitHub Desktop.
Save stringparser/b96966cb26582b3ac6d5 to your computer and use it in GitHub Desktop.
bdd minimal tests
var ansiJS = require('ansi-highlight');
var timer, exitCode;
function format(type, str, fn){
var test, badge;
try {
test = (fn || function(){})() || 'passed';
} catch(err){
test = err;
console.log();
console.log(err.stack);
}
exitCode = exitCode === 1 ? 1 : (test === 'err' ? 1 : 0);
str = ansiJS(str);
if(str === void 0 || str === null || str.trim() === ''){
badge = ' (pending)';
} else if(test === 'passed'){
badge = ' \033[36m✔\033[0m ';
} else if(test instanceof Error){
badge = ' \033[31m✗ ➜\033[0m ';
}
if(type !== 'describe'){
str = ' '+badge+str;
} else if(exitCode === 1){
str = ' \033[31m►\033[0m '+str;
} else {
str = ' \033[36m►\033[0m '+str;
}
if(timer){
clearTimeout(timer);
}
timer = setTimeout(function(){
process.exit(exitCode);
});
return '\n' + str;
}
function describe(/* arguments */){
var args = [].slice.call(arguments);
var fn = args.pop();
var str = args.map(function(arg){
return ''+arg;
}).join(' ');
process.stdout.write(' '+str);
process.stdout.write(format('describe', str, fn));
}
exports.describe = describe;
function it(/* arguments */){
var args = [].slice.call(arguments);
var fn = args.pop();
var str = args.map(function(arg){
return ''+arg;
}).join(' ');
process.stdout.write(format('it', str, fn));
}
exports.it = it;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment