Browse Source

[refactor] bluebird not global, minor refactoring

0.7.0-breaking-rewrite
Muthu Kumar 6 years ago
parent
commit
d03af7278d
  1. 0
      Strategy.js
  2. 10
      src/gunner.js
  3. 1
      src/lib/assertPromise.js
  4. 5
      src/lib/assertionsLibrary.js
  5. 1
      src/lib/expect.js
  6. 17
      src/strategy/index.js
  7. 8
      src/util/constants.js
  8. 9
      src/util/index.js
  9. 1
      src/util/requireDeep.js

0
strategy.js → Strategy.js

10
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');

1
src/lib/assertPromise.js

@ -1,3 +1,4 @@
const Promise = require('bluebird');
const { isPromise } = require('../util');
const createRejectionStatement = (statement, ...args) =>

5
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),

1
src/lib/expect.js

@ -1,3 +1,4 @@
const Promise = require('bluebird');
const { liftPromise } = require('../util');
const _assertPromise = require('./assertPromise');

17
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<string>=} 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)));
}
}

8
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,
};

9
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],
};

1
src/util/requireDeep.js

@ -1,3 +1,4 @@
const Promise = require('bluebird');
const fs = require(`fs`).promises;
const {
map,

Loading…
Cancel
Save