From ed9a23400c787413fc687516efcdb06a4150d238 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Sat, 2 Apr 2016 01:08:50 +0530 Subject: [PATCH] Cleanup printing messages --- index-v1.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- main-runner.js | 4 +++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/index-v1.js b/index-v1.js index faecde2..c1ac26f 100644 --- a/index-v1.js +++ b/index-v1.js @@ -6,7 +6,19 @@ var async = require('async'); var pr = console.log; + var INDENT = ' '; +var STATUS_FLAG = { + true : 'Okey', + false : 'Fail' +}; + +function print( level, item ){ + var indent = getIndent( level ); + var status = STATUS_FLAG[ item.isSuccess ]; + var timeTaken = '(' + ( item.endTime - item.startTime ) + 'ms)'; + pr( [ indent, status, item.description, timeTaken ].join(' ') ); +}; function safeFn( fn ){ return function( done ){ @@ -39,13 +51,27 @@ function mkAsyncFn( fn ){ function ItBlock( description, fn ){ this.description = description; this.fn = mkAsyncFn( fn ); + this.isSuccess = false; } ItBlock.prototype.run = function( done ){ - pr( getIndent( this.parent.level+1 ) + this.description ); - this.fn( done ); + var self = this; + this.startTime = Date.now(); + this.fn( function( err ){ + self.endTime = Date.now(); + if( !err ){ + self.isSuccess = true; + } + self.print(); + done( err ); + }); } +ItBlock.prototype.print = function(){ + print( this.parent.level+1, this ); +}; + + function DescribeBlock( description, parentBlock ){ @@ -63,6 +89,8 @@ function DescribeBlock( description, parentBlock ){ this.its = []; this.beforeEach = null; this.afterEach = null; + + this.isSuccess = false; } @@ -81,6 +109,7 @@ DescribeBlock.prototype.addItBlock = function( description, fn ){ DescribeBlock.prototype.run = function( cb ){ var tasks = []; + var self = this; if( this.beforeFn ){ tasks.push( this.beforeFn ); } @@ -96,9 +125,22 @@ DescribeBlock.prototype.run = function( cb ){ if( this.afterFn ){ tasks.push( this.afterFn ); } + pr( getIndent( this.level) + '---' + this.description + '---' ); + this.startTime = Date.now(); + return async.waterfall( tasks, function( err ){ + + self.endTime = Date.now(); + if( !err ){ + self.isSuccess = true; + } + self.print(); + cb && cb( err ); + }); +} + - pr( getIndent( this.level) + this.description ); - return async.waterfall( tasks, cb ); +DescribeBlock.prototype.print = function(){ + print( this.level, this ); } diff --git a/main-runner.js b/main-runner.js index 42526fb..97f0323 100644 --- a/main-runner.js +++ b/main-runner.js @@ -8,7 +8,9 @@ var SimpleMocha = require( './index-v1' ); var sm = new SimpleMocha(); sm.onLoad = function(){ - sm.rootDescribeBlock.run(); + sm.rootDescribeBlock.run( function( err ){ + console.log( err ? 'Finished with error' : 'Finished Successfully', err ); + }); }