From 0ea99c8d3aa0382ea5ac0ff225ea0476e3eed982 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Wed, 17 Jun 2015 03:31:33 +0530 Subject: [PATCH] Initial commit --- index.js | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 22 +++++++++ 2 files changed, 156 insertions(+) create mode 100644 index.js create mode 100644 package.json diff --git a/index.js b/index.js new file mode 100644 index 0000000..a65246b --- /dev/null +++ b/index.js @@ -0,0 +1,134 @@ +/* + * Simple stupid implementation of mocha test runner. + * It is used to run tests direclty from node. + * Can be used for debugging purpose + * + * 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 printer = function(prefix){ + return function(){ + var args = [].slice.call(arguments); + args[0] = prefix + args[0]; + console.log.apply(console, args ); + }; +}; + + +function runner( d, cb ){ + var print = printer( d.indent ); + cb = cb||function(){}; + print( 'Describe: ', d.name ); + + return async.series([ + + /* First before hook */ + function(cb){ + if(!d.before){ return cb(); } + print( ' Before hook' ); + return d.before(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); + } + print( ' It: ' + itItem.name ); + + if( !itItem.fn ){ return cb(); } + + if( itItem.fn.length ){ return itItem.fn(cb); } + + itItem.fn(); + return cb(); + }, cb ); + }, + + /* Then after hook */ + function(cb){ + if(!d.after){ return cb(); } + print( ' After hook' ); + return d.after(cb); + }, + + ], function(err){ + print( 'Finished with ' + ( err? '': 'No' ) + ' Error' ); + if(err){ + print( 'Error ', err.stack ); + } + return cb(); + }); +} + + +var store = { + currentDescribe: null, +}; + + +var describe = function(str, fn ){ + var parentDesc = store.currentDescribe; + var descItem = { + name: str, + its: [], + indent: ' ' + ( parentDesc? parentDesc.indent : '' ), + }; + store.currentDescribe = descItem; + + fn(); + + if( parentDesc ){ + parentDesc.its.push( descItem ); + store.currentDescribe = parentDesc; + } else { + runner(descItem); + } +}; + + + +var it = function( str, fn ){ + + store.currentDescribe.its.push({ + name : str, + fn: fn + }); +}; +it.skip = function(){}; + + +var before = function( fn ){ + store.currentDescribe.before = fn; +}; + + +var after = function( fn ){ + store.currentDescribe.after = fn; +}; + + +global.after = after; +global.describe = describe; +global.it = it; +global.before = before; +global.runner = runner; + diff --git a/package.json b/package.json new file mode 100644 index 0000000..e64562e --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "simple-mocha", + "version": "0.0.1", + "description": "Simple stupid implementation of Mocha test runner. Tests can be run directly within node as any Nodejs code.", + "author": "Harish.K", + "license": "MIT", + "keywords": [ + "mocha", + "test runner", + "testing" + ], + "repository": { + "type": "git", + "url": "git@github.com:harish2704/simple-mocha.git" + }, + "main": "./index.js", + "dependencies": { + "async": "~1.2.1" + }, + "devDependencies": {}, + "scripts": {} +}