mirror of
https://github.com/harish2704/simple-mocha.git
synced 2026-09-10 06:21:08 +00:00
* Refactor.
* Features: * collect execution time, * handle errors Signed-off-by: Harish.K <harish2704@gmail.com>
This commit is contained in:
@@ -19,19 +19,60 @@ if( global.describe ){ return; }
|
|||||||
|
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
|
||||||
|
function padding( str, width ){
|
||||||
|
return ( ' ' + str ).slice(-width);
|
||||||
|
}
|
||||||
|
|
||||||
var printer = function(prefix){
|
|
||||||
return function(){
|
|
||||||
var args = [].slice.call(arguments);
|
function Task(name, fn, parentNode ){
|
||||||
args[0] = prefix + args[0];
|
this.parentNode = parentNode;
|
||||||
console.log.apply(console, args );
|
this.name = name;
|
||||||
|
this.fn = fn;
|
||||||
|
this.isAsync = fn && fn.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
Task.prototype.exec = function(_cb){
|
||||||
|
if( !this.fn ){ return _cb(); }
|
||||||
|
var _this = this;
|
||||||
|
var cb = function(err){
|
||||||
|
_this.endTime = Date.now();
|
||||||
|
if( err ){
|
||||||
|
_this.err = err;
|
||||||
|
_this.parentNode.errorCount++;
|
||||||
|
}
|
||||||
|
_this.print();
|
||||||
|
_cb();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.startTime = Date.now();
|
||||||
|
if( this.isAsync ){
|
||||||
|
return this.fn(cb);
|
||||||
|
}
|
||||||
|
try{
|
||||||
|
this.fn();
|
||||||
|
cb();
|
||||||
|
} catch(e){
|
||||||
|
cb(e);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
Task.prototype.print = function(){
|
||||||
|
var timeTaken = this.endTime - this.startTime;
|
||||||
|
console.log(
|
||||||
|
this.parentNode.indent +
|
||||||
|
' It: ' +
|
||||||
|
( this.err? 'Fail ' : ' OK ' )+
|
||||||
|
padding( '(' + timeTaken + 'ms) ' , 11) +
|
||||||
|
this.name
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function runner( d, cb ){
|
function runner( d, cb ){
|
||||||
var print = printer( d.indent );
|
|
||||||
cb = cb||function(){};
|
cb = cb||function(){};
|
||||||
|
console.log('');
|
||||||
print( 'Describe: ', d.name );
|
print( 'Describe: ', d.name );
|
||||||
|
|
||||||
return async.series([
|
return async.series([
|
||||||
@@ -52,14 +93,8 @@ function runner( d, cb ){
|
|||||||
if( itItem.its ){
|
if( itItem.its ){
|
||||||
return runner(itItem, cb);
|
return runner(itItem, cb);
|
||||||
}
|
}
|
||||||
print( ' It: ' + itItem.name );
|
|
||||||
|
|
||||||
if( !itItem.fn ){ return cb(); }
|
return itItem.exec(cb);
|
||||||
|
|
||||||
if( itItem.fn.length ){ return itItem.fn(cb); }
|
|
||||||
|
|
||||||
itItem.fn();
|
|
||||||
return cb();
|
|
||||||
}, cb );
|
}, cb );
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -70,11 +105,11 @@ function runner( d, cb ){
|
|||||||
return d.after(cb);
|
return d.after(cb);
|
||||||
},
|
},
|
||||||
|
|
||||||
], function(err){
|
], function(){
|
||||||
print( 'Finished with ' + ( err? '': 'No' ) + ' Error' );
|
if( d.parentNode ){
|
||||||
if(err){
|
d.parentNode.errorCount += d.errorCount;
|
||||||
print( 'Error ', err.stack );
|
|
||||||
}
|
}
|
||||||
|
print( 'Finished with ' + ( d.errorCount || 'No' ) + ' Error' );
|
||||||
return cb();
|
return cb();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -86,19 +121,21 @@ var store = {
|
|||||||
|
|
||||||
|
|
||||||
var describe = function(str, fn ){
|
var describe = function(str, fn ){
|
||||||
var parentDesc = store.currentDescribe;
|
var parentNode = store.currentDescribe;
|
||||||
var descItem = {
|
var descItem = {
|
||||||
name: str,
|
name: str,
|
||||||
its: [],
|
its: [],
|
||||||
indent: ' ' + ( parentDesc? parentDesc.indent : '' ),
|
indent: ' ' + ( parentNode? parentNode.indent : '' ),
|
||||||
|
errorCount: 0,
|
||||||
|
parentNode: parentNode,
|
||||||
};
|
};
|
||||||
store.currentDescribe = descItem;
|
store.currentDescribe = descItem;
|
||||||
|
|
||||||
fn();
|
fn();
|
||||||
|
|
||||||
if( parentDesc ){
|
if( parentNode ){
|
||||||
parentDesc.its.push( descItem );
|
parentNode.its.push( descItem );
|
||||||
store.currentDescribe = parentDesc;
|
store.currentDescribe = parentNode;
|
||||||
} else {
|
} else {
|
||||||
runner(descItem);
|
runner(descItem);
|
||||||
}
|
}
|
||||||
@@ -107,11 +144,8 @@ var describe = function(str, fn ){
|
|||||||
|
|
||||||
|
|
||||||
var it = function( str, fn ){
|
var it = function( str, fn ){
|
||||||
|
var task = new Task(str, fn, store.currentDescribe );
|
||||||
store.currentDescribe.its.push({
|
store.currentDescribe.its.push( task );
|
||||||
name : str,
|
|
||||||
fn: fn
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
it.skip = function(){};
|
it.skip = function(){};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user