From 9dcf02dcfba9e5da6dca930d444394b96302fea7 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Wed, 17 Jun 2015 13:48:16 +0530 Subject: [PATCH 1/5] * Refactor. * Features: * collect execution time, * handle errors Signed-off-by: Harish.K --- index.js | 88 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index a65246b..be5f6e3 100644 --- a/index.js +++ b/index.js @@ -19,19 +19,60 @@ if( global.describe ){ return; } var async = require('async'); +function padding( str, width ){ + return ( ' ' + str ).slice(-width); +} + + + +function Task(name, fn, parentNode ){ + this.parentNode = parentNode; + this.name = name; + this.fn = fn; + this.isAsync = fn && fn.length; +} -var printer = function(prefix){ - return function(){ - var args = [].slice.call(arguments); - args[0] = prefix + args[0]; - console.log.apply(console, args ); +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 ){ - var print = printer( d.indent ); cb = cb||function(){}; + console.log(''); print( 'Describe: ', d.name ); return async.series([ @@ -52,14 +93,8 @@ function runner( d, cb ){ if( itItem.its ){ return runner(itItem, cb); } - print( ' It: ' + itItem.name ); - - if( !itItem.fn ){ return cb(); } - if( itItem.fn.length ){ return itItem.fn(cb); } - - itItem.fn(); - return cb(); + return itItem.exec(cb); }, cb ); }, @@ -70,11 +105,11 @@ function runner( d, cb ){ return d.after(cb); }, - ], function(err){ - print( 'Finished with ' + ( err? '': 'No' ) + ' Error' ); - if(err){ - print( 'Error ', err.stack ); + ], function(){ + if( d.parentNode ){ + d.parentNode.errorCount += d.errorCount; } + print( 'Finished with ' + ( d.errorCount || 'No' ) + ' Error' ); return cb(); }); } @@ -86,19 +121,21 @@ var store = { var describe = function(str, fn ){ - var parentDesc = store.currentDescribe; + var parentNode = store.currentDescribe; var descItem = { name: str, its: [], - indent: ' ' + ( parentDesc? parentDesc.indent : '' ), + indent: ' ' + ( parentNode? parentNode.indent : '' ), + errorCount: 0, + parentNode: parentNode, }; store.currentDescribe = descItem; fn(); - if( parentDesc ){ - parentDesc.its.push( descItem ); - store.currentDescribe = parentDesc; + if( parentNode ){ + parentNode.its.push( descItem ); + store.currentDescribe = parentNode; } else { runner(descItem); } @@ -107,11 +144,8 @@ var describe = function(str, fn ){ var it = function( str, fn ){ - - store.currentDescribe.its.push({ - name : str, - fn: fn - }); + var task = new Task(str, fn, store.currentDescribe ); + store.currentDescribe.its.push( task ); }; it.skip = function(){}; From 29c671188b2a8491d17cde884a53b41daf83a47a Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Wed, 17 Jun 2015 13:48:56 +0530 Subject: [PATCH 2/5] V 0.0.2 :) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e64562e..0548303 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simple-mocha", - "version": "0.0.1", + "version": "0.0.2", "description": "Simple stupid implementation of Mocha test runner. Tests can be run directly within node as any Nodejs code.", "author": "Harish.K", "license": "MIT", From e0a988709f4522470243afd9642e302b63fc3192 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Wed, 17 Jun 2015 14:09:18 +0530 Subject: [PATCH 3/5] Fixes: --- index.js | 10 +++++++++- package.json | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index be5f6e3..dffd480 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ /* Don't do anything if we are in Mocha's environment */ -if( global.describe ){ return; } +// if( global.describe ){ return; } @@ -23,6 +23,13 @@ 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 ){ @@ -72,6 +79,7 @@ Task.prototype.print = function(){ function runner( d, cb ){ cb = cb||function(){}; + var print = printer( d.indent ); console.log(''); print( 'Describe: ', d.name ); diff --git a/package.json b/package.json index 0548303..23c88ed 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simple-mocha", - "version": "0.0.2", + "version": "0.0.3", "description": "Simple stupid implementation of Mocha test runner. Tests can be run directly within node as any Nodejs code.", "author": "Harish.K", "license": "MIT", From f0fb70917a39b866d5e52c60a4cd085a41eea4cd Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Wed, 17 Jun 2015 17:20:32 +0530 Subject: [PATCH 4/5] Fix: print stacktrace --- index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.js b/index.js index dffd480..5c73a62 100644 --- a/index.js +++ b/index.js @@ -74,6 +74,10 @@ Task.prototype.print = function(){ padding( '(' + timeTaken + 'ms) ' , 11) + this.name ); + + if( this.err ){ + console.log( this.err.stack ); + } }; From 83cb1e0c46e59a139836738fda2acef850494e71 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Sat, 20 Jun 2015 22:04:27 +0530 Subject: [PATCH 5/5] Fix Error management. --- index.js | 18 ++++++++++++------ package.json | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 5c73a62..c1b14d3 100644 --- a/index.js +++ b/index.js @@ -53,12 +53,13 @@ Task.prototype.exec = function(_cb){ }; this.startTime = Date.now(); - if( this.isAsync ){ - return this.fn(cb); - } try{ - this.fn(); - cb(); + if( this.isAsync ){ + this.fn(cb); + } else { + this.fn(); + cb(); + } } catch(e){ cb(e); } @@ -76,7 +77,12 @@ Task.prototype.print = function(){ ); if( this.err ){ - console.log( this.err.stack ); + if(this.err instanceof Error ){ + // console.log( this.err.stack ); + console.log( this.err ); + }else{ + console.log( this.err ); + } } }; diff --git a/package.json b/package.json index 23c88ed..0cb9960 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simple-mocha", - "version": "0.0.3", + "version": "0.0.4", "description": "Simple stupid implementation of Mocha test runner. Tests can be run directly within node as any Nodejs code.", "author": "Harish.K", "license": "MIT",