From a545b6da6f7f818f79d6781014653c3d5cf999f0 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Fri, 1 Apr 2016 23:05:57 +0530 Subject: [PATCH] * Refactoring utils * second level block parsing tests --- test/data/dummy.spec.js | 72 +++++++++------------------------------ test/data/utils.js | 64 ++++++++++++++++++++++++++++++++++ test/simple-mocha.spec.js | 37 ++++++++++++++++++++ 3 files changed, 117 insertions(+), 56 deletions(-) create mode 100644 test/data/utils.js diff --git a/test/data/dummy.spec.js b/test/data/dummy.spec.js index e243dda..df451ab 100644 --- a/test/data/dummy.spec.js +++ b/test/data/dummy.spec.js @@ -1,64 +1,16 @@ /* globals describe, it, before, after, beforeEach, afterEach */ -var messageLog = []; -var pr = function( msg ){ - messageLog.push( msg ); - // console.log( '--', msg ); -} -function asyncFn( args ){ - return function( cb ){ - setTimeout( function(){ - pr( args.msg ); - if( args.error ){ - if( args.isThrow ){ - throw args.error; - } else { - return cb( args.error ); - } - } else { - return cb( ); - } - }); - } -} +var utils = require( './test/data/utils' ); +var resolvingFn = utils.resolvingFn; -var beforeFn = asyncFn({ - msg: 'Before_block', - delay: 100, - error: null, -}); - -var it1 = asyncFn({ - msg: 'It_1', - delay: 10, - error: null, -}); - -var it2 = asyncFn({ - msg: 'It_2', - delay: 500, - error: null, -}); - -var afterFn = asyncFn({ - msg: 'After_block', - delay: 60, - error: null, -}); - -var beforeEachFn = asyncFn({ - msg: 'BeforeEach_block', - delay: 5, - error: null -}); - -var afterEachFn = asyncFn({ - msg: 'AfterEach_block', - delay: 5, - error: null -}); +var beforeFn = resolvingFn( 'Before_block' ); +var it1 = resolvingFn( 'It_1' ); +var it2 = resolvingFn( 'It_2' ); +var afterFn = resolvingFn( 'After_block' ); +var beforeEachFn = resolvingFn( 'BeforeEach_block' ); +var afterEachFn = resolvingFn( 'AfterEach_block' ); describe( 'describe', function(){ @@ -70,5 +22,13 @@ describe( 'describe', function(){ it( 'it2', it2 ); after( afterFn ); + describe( 'child block', function(){ + before( beforeFn ); + beforeEach( beforeEachFn ); + afterEach( afterEachFn ); + it( 'it1', it1 ); + it( 'it2', it2 ); + after( afterFn ); + }); }); diff --git a/test/data/utils.js b/test/data/utils.js new file mode 100644 index 0000000..f60031a --- /dev/null +++ b/test/data/utils.js @@ -0,0 +1,64 @@ + +var messageLog = []; + +var pr = function( msg ){ + messageLog.push( msg ); + // console.log( '--', msg ); +} + + +function asyncFn( args ){ + return function( cb ){ + setTimeout( function(){ + pr( args.msg ); + if( args.error ){ + if( args.isThrow ){ + throw args.error; + } else { + return cb( args.error ); + } + } else { + return cb( ); + } + }); + } +} + +function rand(){ + return parseInt( Math.random()*1000 ) % 27; +} + +function resolvingFn( msg ){ + return asyncFn({ + msg: msg, + delay: rand(), + error: null, + }); +} + +function rejectingFn( msg ){ + return asyncFn({ + msg: msg, + delay: rand(), + error: new Error( 'Reject:' + msg ), + }); +} + +function throwingFn( msg ){ + return asyncFn({ + msg: msg, + delay: rand(), + error: new Error( 'Throw:' + msg ), + isThrow: true, + }); +} + + +module.exports = { + messageLog: messageLog, + pr: pr, + asyncFn: asyncFn, + resolvingFn: resolvingFn, + rejectingFn: rejectingFn, + throwingFn: throwingFn, +}; diff --git a/test/simple-mocha.spec.js b/test/simple-mocha.spec.js index 1d48850..d9bd0ad 100644 --- a/test/simple-mocha.spec.js +++ b/test/simple-mocha.spec.js @@ -61,6 +61,43 @@ describe( 'Simple before block + it s ', function(){ }) }); + + describe( 'second level child block', function( ){ + + it( 'should have second level describeBlock', function( ){ + assert.equal( firsDescribeBlock.children.length, 1 ); + assert( firsDescribeBlock.children[0] ); + }); + + var childDescribeBlock = firsDescribeBlock.children[0]; + + it( 'should parse before hook', function(){ + assert( childDescribeBlock.beforeFn ); + }); + + + it( 'should parse it blocks', function(){ + assert.equal( childDescribeBlock.its.length, 2 ); + childDescribeBlock.its.forEach( function( itBlk ){ + assert( itBlk ); + assert( itBlk.description ); + assert( itBlk.fn ); + }); + }); + + + it( 'should parse after blocks', function(){ + assert( childDescribeBlock.afterFn ); + }); + + it( 'should parse beforeEach block', function(){ + assert( childDescribeBlock.beforeEachFn ); + }) + + it( 'should parse afterEach block', function(){ + assert( childDescribeBlock.afterEachFn ); + }) + }) }); }); })