From e01d0c5dacd55e5b068409406fd08c4aa64b0b0b Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Sun, 3 Apr 2016 15:34:54 +0530 Subject: [PATCH] Cleanups --- index.js | 184 +-------------------------------------------- src/main-runner.js | 2 +- 2 files changed, 2 insertions(+), 184 deletions(-) diff --git a/index.js b/index.js index 88f9091..45accce 100644 --- a/index.js +++ b/index.js @@ -6,186 +6,4 @@ * By Harish.K */ -/* - * 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; - +module.exports = require( './src/main-runner' ); diff --git a/src/main-runner.js b/src/main-runner.js index 2479e23..416c827 100644 --- a/src/main-runner.js +++ b/src/main-runner.js @@ -2,7 +2,7 @@ var context = global; if( context.describe ){ - return; + return; // Don't do anything if mocha any other bdd-runner is already loaded } var SimpleMocha = require( './simple-mocha' ); var sm = new SimpleMocha();