From 9051ebd8c24e68d5c055c1b49a539aa03139009a Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 4 Sep 2018 12:50:53 +0530 Subject: [PATCH] [runtests] Added unitReducer --- package.json | 2 +- src/lib/runTests.js | 87 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 55 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 0e96ba2..d98d822 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@klenty/gunner", - "version": "0.6.6", + "version": "0.6.7", "description": "Zero magic, fast test-runner and assertion framework. No magic globals.", "main": "index.js", "repository": { diff --git a/src/lib/runTests.js b/src/lib/runTests.js index c59ba5c..073a8a0 100644 --- a/src/lib/runTests.js +++ b/src/lib/runTests.js @@ -1,5 +1,7 @@ 'use strict'; +const Promise = require('bluebird'); +Promise.object = require('@codefeathers/promise.object'); const chalk = require('chalk'); const logger = require('./logger'); @@ -26,38 +28,56 @@ const snipStack = e => { }; +const unitReducer = + (units = [], stateMark) => + (state = {}) => + units.reduce( + (accumulator, unit) => + accumulator + .then(thisState => + Promise.resolve( + unit.run({ ...state, [stateMark]: thisState }) + ) + .then(newState => + [ ...thisState, newState ])), + Promise.resolve(state[stateMark] || []), + ); + const runTests = (instance, options) => { const log = logger.create(options); - const beforeAll = () => Promise.map( - [ - ...instance.__hooks__.before[constants.Start] || [], - ...instance.__hooks__.after[constants.Start] || [], - ], - hook => hook.run(), - ); - - const beforeEvery = state => Promise.mapSeries( - instance.__hooks__.before['*'] || [], - hook => hook.run(state), - ); + const beforeAll = () => + unitReducer( + [ + ...(instance.__hooks__.before[constants.Start] || []), + ...(instance.__hooks__.after[constants.Start] || []), + ], + '@start', + )(); + + const beforeEvery = state => + unitReducer( + instance.__hooks__.before['*'], + '@every', + )(state); const runner = state => Promise.mapSeries(instance.__tests__, each => { - const beforeThis = state => Promise.mapSeries( - instance.__hooks__.before[each.description] || [], - hook => hook.run(state), - ); + const beforeThis = + unitReducer( + instance.__hooks__.before[each.description], + '@this' + ); - const afterThis = state => Promise.mapSeries( - instance.__hooks__.after[each.description] || [], - hook => hook.run(state), - ); + const afterThis = + unitReducer( + instance.__hooks__.after[each.description], + '@afterThis' + ); - return beforeEvery(state) - .then(newState => ({ ...state, '@every': newState })) - .then(state => Promise.object({ ...state, '@this': beforeThis() })) + return Promise.object({ ...state, '@every': beforeEvery(state) }) + .then(state => Promise.object({ ...state, '@this': beforeThis(state) })) .then(state => { const pred = each.test(state); @@ -75,7 +95,7 @@ const runTests = (instance, options) => { ? Promise.all(pred) : pred.then(x => Array.isArray(x) ? Promise.all(x) : x); - return [ + return ([ state, toTest .then(() => { @@ -104,20 +124,21 @@ const runTests = (instance, options) => { error, }; }), - ]; + ]); }) - .spread((state, result) => afterThis(state).then(() => result)); + .then(([state, result]) => afterThis(state).then(() => result)); }); - const afterAll = state => Promise.mapSeries( - [ - ...(instance.__hooks__.before[constants.End] || []), - ...(instance.__hooks__.after[constants.End] || []), - ], - hook => hook.run(state, state['@results']), - ); + const afterAll = + unitReducer( + [ + ...(instance.__hooks__.before[constants.End] || []), + ...(instance.__hooks__.after[constants.End] || []), + ], + '@after-all', + ); return Promise.object({ '@start': beforeAll() }) .then(state => Promise.object({ ...state, '@results': runner(state)}))