mirror of
https://github.com/harish2704/simple-mocha.git
synced 2026-09-10 06:21:08 +00:00
Cleanups
This commit is contained in:
@@ -6,186 +6,4 @@
|
|||||||
* By Harish.K<harish2704@gmail.com>
|
* By Harish.K<harish2704@gmail.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
module.exports = require( './src/main-runner' );
|
||||||
* TODO: beforeEach hook is not implemented.
|
|
||||||
* only 'before' and 'after' hook is implemented
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/* Don't do anything if we are in Mocha's environment */
|
|
||||||
if( global.describe ){ return; }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var async = require('async');
|
|
||||||
var TXT_OK = '\u001b[32m\u001b[1m' + 'Okey' + '\u001b[22m\u001b[39m';
|
|
||||||
var TXT_FAIL = '\u001b[31m\u001b[1m' + 'Fail' + '\u001b[22m\u001b[39m';
|
|
||||||
|
|
||||||
function padding( str, width ){
|
|
||||||
return ( ' ' + str ).slice(-width);
|
|
||||||
}
|
|
||||||
|
|
||||||
var printer = function(prefix){
|
|
||||||
return function(){
|
|
||||||
var args = [].slice.call(arguments);
|
|
||||||
args[0] = prefix + args[0];
|
|
||||||
console.log.apply(console, args );
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
function Task(name, fn, parentNode ){
|
|
||||||
this.parentNode = parentNode;
|
|
||||||
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(err);
|
|
||||||
};
|
|
||||||
|
|
||||||
this.startTime = Date.now();
|
|
||||||
if( this.isAsync ){
|
|
||||||
try{
|
|
||||||
this.fn(cb);
|
|
||||||
} catch(e){
|
|
||||||
cb(e);
|
|
||||||
} finally{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try{
|
|
||||||
this.fn();
|
|
||||||
cb();
|
|
||||||
} catch(e){
|
|
||||||
cb(e);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
Task.prototype.print = function(){
|
|
||||||
var timeTaken = this.endTime - this.startTime;
|
|
||||||
var err = this.err;
|
|
||||||
console.log(
|
|
||||||
( err ? TXT_FAIL : TXT_OK ) +
|
|
||||||
' '+
|
|
||||||
this.parentNode.indent +
|
|
||||||
this.name +
|
|
||||||
padding( '(' + timeTaken + 'ms) ' , 11));
|
|
||||||
if(err){
|
|
||||||
console.log('');
|
|
||||||
console.log( err.stack );
|
|
||||||
console.log('');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
function runner( d, cb ){
|
|
||||||
cb = cb||function(){};
|
|
||||||
var print = printer( d.indent );
|
|
||||||
console.log('');
|
|
||||||
print( 'Describe: ', d.name );
|
|
||||||
|
|
||||||
return async.series([
|
|
||||||
|
|
||||||
/* First before hook */
|
|
||||||
function(cb){
|
|
||||||
if(!d.before){ return cb(); }
|
|
||||||
print( ' Before hook' );
|
|
||||||
return d.before.exec(cb);
|
|
||||||
},
|
|
||||||
|
|
||||||
/* Then each It functions */
|
|
||||||
function(cb){
|
|
||||||
async.eachSeries(d.its,
|
|
||||||
function(itItem, cb ){
|
|
||||||
|
|
||||||
// Check whether it is 'IT' item or 'DESCRIBE' item
|
|
||||||
if( itItem.its ){
|
|
||||||
return runner(itItem, cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
return itItem.exec(cb);
|
|
||||||
}, cb );
|
|
||||||
},
|
|
||||||
|
|
||||||
/* Then after hook */
|
|
||||||
function(cb){
|
|
||||||
if(!d.after){ return cb(); }
|
|
||||||
print( ' After hook' );
|
|
||||||
return d.after.exec(cb);
|
|
||||||
},
|
|
||||||
|
|
||||||
], function( ){
|
|
||||||
if( d.parentNode ){
|
|
||||||
d.parentNode.errorCount += d.errorCount;
|
|
||||||
}
|
|
||||||
print( 'Finished with ' + ( d.errorCount || 'No' ) + ' Error' );
|
|
||||||
return cb();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var store = {
|
|
||||||
currentDescribe: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
var describe = function(str, fn ){
|
|
||||||
var parentNode = store.currentDescribe;
|
|
||||||
var descItem = {
|
|
||||||
name: str,
|
|
||||||
its: [],
|
|
||||||
indent: ' ' + ( parentNode? parentNode.indent : '' ),
|
|
||||||
errorCount: 0,
|
|
||||||
parentNode: parentNode,
|
|
||||||
};
|
|
||||||
store.currentDescribe = descItem;
|
|
||||||
|
|
||||||
fn();
|
|
||||||
|
|
||||||
if( parentNode ){
|
|
||||||
parentNode.its.push( descItem );
|
|
||||||
store.currentDescribe = parentNode;
|
|
||||||
} else {
|
|
||||||
runner(descItem);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var it = function( str, fn ){
|
|
||||||
var task = new Task(str, fn, store.currentDescribe );
|
|
||||||
store.currentDescribe.its.push( task );
|
|
||||||
};
|
|
||||||
it.skip = function(){};
|
|
||||||
|
|
||||||
|
|
||||||
var before = function( fn ){
|
|
||||||
var task = new Task('Before hook', fn, store.currentDescribe );
|
|
||||||
store.currentDescribe.before = task;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
var after = function( fn ){
|
|
||||||
var task = new Task('After hook', fn, store.currentDescribe );
|
|
||||||
store.currentDescribe.after = task;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
global.after = after;
|
|
||||||
global.describe = describe;
|
|
||||||
global.it = it;
|
|
||||||
global.before = before;
|
|
||||||
global.runner = runner;
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
var context = global;
|
var context = global;
|
||||||
if( context.describe ){
|
if( context.describe ){
|
||||||
return;
|
return; // Don't do anything if mocha any other bdd-runner is already loaded
|
||||||
}
|
}
|
||||||
var SimpleMocha = require( './simple-mocha' );
|
var SimpleMocha = require( './simple-mocha' );
|
||||||
var sm = new SimpleMocha();
|
var sm = new SimpleMocha();
|
||||||
|
|||||||
Reference in New Issue
Block a user