From d03af7278d649d5cc427593a7f79f8c4da75e76b Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 4 Sep 2018 12:48:53 +0530 Subject: [PATCH] [refactor] bluebird not global, minor refactoring --- Strategy.js | 1 + src/gunner.js | 10 +++++----- src/lib/assertPromise.js | 1 + src/lib/assertionsLibrary.js | 5 +++++ src/lib/expect.js | 1 + src/strategy/index.js | 17 ++++++++++------- src/util/constants.js | 8 ++++++++ src/util/index.js | 9 +++++++++ src/util/requireDeep.js | 1 + strategy.js | 1 - 10 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 Strategy.js create mode 100644 src/util/constants.js delete mode 100644 strategy.js diff --git a/Strategy.js b/Strategy.js new file mode 100644 index 0000000..15c9ab4 --- /dev/null +++ b/Strategy.js @@ -0,0 +1 @@ +module.exports = require('./src/strategy'); diff --git a/src/gunner.js b/src/gunner.js index f80c111..0a7c2f9 100644 --- a/src/gunner.js +++ b/src/gunner.js @@ -3,14 +3,13 @@ const { EOL } = require('os'); const chalk = require('chalk'); -Promise = require('bluebird'); +const Promise = require('bluebird'); Promise.object = require('@codefeathers/promise.object'); const _runTests = require('./lib/runTests'); const _expect = require('./lib/expect'); const logger = require('./lib/logger'); -const { hasProp } = require('./util'); const symbols = require('./util/symbols'); class Gunner { @@ -19,10 +18,12 @@ class Gunner { this.__hooks__ = { before: { [symbols.Start]: [], - [symbols.Stop]: [], + [symbols.End]: [], '*': [], }, after: { + [symbols.Start]: [], + [symbols.End]: [], '*': [], }, }; @@ -41,7 +42,7 @@ class Gunner { this.__tests__.push({ description, - test: (state) => { + test: state => { try { return test(_expect, state); } catch (e) { @@ -81,7 +82,6 @@ class Gunner { } run (options = {}) { - options.log = (options || {})['log'] || !(hasProp(options)('log')); return _runTests(this, options) .then(results => { const success = results.filter(r => r.result === 'pass'); diff --git a/src/lib/assertPromise.js b/src/lib/assertPromise.js index 2676309..eb58992 100644 --- a/src/lib/assertPromise.js +++ b/src/lib/assertPromise.js @@ -1,3 +1,4 @@ +const Promise = require('bluebird'); const { isPromise } = require('../util'); const createRejectionStatement = (statement, ...args) => diff --git a/src/lib/assertionsLibrary.js b/src/lib/assertionsLibrary.js index a84a4ae..d84a35f 100644 --- a/src/lib/assertionsLibrary.js +++ b/src/lib/assertionsLibrary.js @@ -10,6 +10,11 @@ module.exports.fail = [ () => false, () => null, ]; +module.exports.exists = + [ + val => typeof val !== 'undefined', + () => `Value is undefined` + ]; module.exports.isArray = [ val => Array.isArray(val), diff --git a/src/lib/expect.js b/src/lib/expect.js index 7e0138b..e73546e 100644 --- a/src/lib/expect.js +++ b/src/lib/expect.js @@ -1,3 +1,4 @@ +const Promise = require('bluebird'); const { liftPromise } = require('../util'); const _assertPromise = require('./assertPromise'); diff --git a/src/strategy/index.js b/src/strategy/index.js index c233781..c7cf5c2 100644 --- a/src/strategy/index.js +++ b/src/strategy/index.js @@ -1,5 +1,7 @@ const Promise = require('bluebird'); + const requireDeep = require('../util/requireDeep'); +const Runner = require('../runner'); class Strategy { @@ -14,6 +16,7 @@ class Strategy { this.__resourceCreators = resources; this.__runTimeOptions = runTimeOptions; this.compiler = compiler; + this.__await__ = []; this.__gunnerInstances = []; this.resources = {}; @@ -37,11 +40,9 @@ class Strategy { * @param {string|Array=} options.pattern */ fetchSpecs (options) { - this.__gunnerInstances = - Promise.map( - requireDeep(options), - this.compiler(this), - ); + this.__await__.push(Promise.map(requireDeep(options), each => { + this.__gunnerInstances = this.compiler(this)(each); + })); return this; } @@ -59,8 +60,10 @@ class Strategy { * @param {Object=} options */ run (options) { - return Promise.map(this.__gunnerInstances, instance => - instance.run(options || this.__runTimeOptions)); + const runOptions = options || this.__runTimeOptions; + + return Promise.all(this.__await__).then(() => ( + Runner(this.__gunnerInstances)(runOptions))); } } diff --git a/src/util/constants.js b/src/util/constants.js new file mode 100644 index 0000000..c03051e --- /dev/null +++ b/src/util/constants.js @@ -0,0 +1,8 @@ +const chalk = require('chalk'); +const { EOL } = require('os'); + +module.exports = { + greenLine: chalk.green('------------------------------------'), + redLine: chalk.red('------------------------------------'), + EOL, +}; diff --git a/src/util/index.js b/src/util/index.js index 6947c03..f76d690 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -7,6 +7,9 @@ const stringify = obj => ? (obj.stack || _stringify(obj)) : obj; +const deepFlatten = arr => [].concat( + ...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v))); + /* Returns true if a promise is passed */ const isPromise = prom => prom && (typeof prom.then === 'function'); @@ -40,6 +43,9 @@ module.exports = { /* Flattens an array of arrays to an array */ flatten : arrData => [].concat.apply([], arrData), + /* Deep flattens arrays */ + deepFlatten, + /* Maps a function over an array */ map : fn => x => x.map(fn), @@ -83,4 +89,7 @@ module.exports = { /* Check if object has given property */ hasProp : obj => prop => prop in obj, + /* Fetches last element from list */ + last : arr => arr[arr.length - 1], + }; diff --git a/src/util/requireDeep.js b/src/util/requireDeep.js index e58d929..1a3d4b7 100644 --- a/src/util/requireDeep.js +++ b/src/util/requireDeep.js @@ -1,3 +1,4 @@ +const Promise = require('bluebird'); const fs = require(`fs`).promises; const { map, diff --git a/strategy.js b/strategy.js deleted file mode 100644 index 15c9ab4..0000000 --- a/strategy.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./src/strategy');