diff --git a/strategy.js b/Strategy.js similarity index 100% rename from strategy.js rename to Strategy.js 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,