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 { EOL } = require('os');
const chalk = require('chalk'); const chalk = require('chalk');
Promise = require('bluebird'); const Promise = require('bluebird');
Promise.object = require('@codefeathers/promise.object'); Promise.object = require('@codefeathers/promise.object');
const _runTests = require('./lib/runTests'); const _runTests = require('./lib/runTests');
const _expect = require('./lib/expect'); const _expect = require('./lib/expect');
const logger = require('./lib/logger'); const logger = require('./lib/logger');
const { hasProp } = require('./util');
const symbols = require('./util/symbols'); const symbols = require('./util/symbols');
class Gunner { class Gunner {
@ -19,10 +18,12 @@ class Gunner {
this.__hooks__ = { this.__hooks__ = {
before: { before: {
[symbols.Start]: [], [symbols.Start]: [],
[symbols.Stop]: [], [symbols.End]: [],
'*': [], '*': [],
}, },
after: { after: {
[symbols.Start]: [],
[symbols.End]: [],
'*': [], '*': [],
}, },
}; };
@ -41,7 +42,7 @@ class Gunner {
this.__tests__.push({ this.__tests__.push({
description, description,
test: (state) => { test: state => {
try { try {
return test(_expect, state); return test(_expect, state);
} catch (e) { } catch (e) {
@ -81,7 +82,6 @@ class Gunner {
} }
run (options = {}) { run (options = {}) {
options.log = (options || {})['log'] || !(hasProp(options)('log'));
return _runTests(this, options) return _runTests(this, options)
.then(results => { .then(results => {
const success = results.filter(r => r.result === 'pass'); 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 { isPromise } = require('../util');
const createRejectionStatement = (statement, ...args) => const createRejectionStatement = (statement, ...args) =>

5
src/lib/assertionsLibrary.js

@ -10,6 +10,11 @@ module.exports.fail = [
() => false, () => false,
() => null, () => null,
]; ];
module.exports.exists =
[
val => typeof val !== 'undefined',
() => `Value is undefined`
];
module.exports.isArray = module.exports.isArray =
[ [
val => Array.isArray(val), val => Array.isArray(val),

1
src/lib/expect.js

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

17
src/strategy/index.js

@ -1,5 +1,7 @@
const Promise = require('bluebird'); const Promise = require('bluebird');
const requireDeep = require('../util/requireDeep'); const requireDeep = require('../util/requireDeep');
const Runner = require('../runner');
class Strategy { class Strategy {
@ -14,6 +16,7 @@ class Strategy {
this.__resourceCreators = resources; this.__resourceCreators = resources;
this.__runTimeOptions = runTimeOptions; this.__runTimeOptions = runTimeOptions;
this.compiler = compiler; this.compiler = compiler;
this.__await__ = [];
this.__gunnerInstances = []; this.__gunnerInstances = [];
this.resources = {}; this.resources = {};
@ -37,11 +40,9 @@ class Strategy {
* @param {string|Array<string>=} options.pattern * @param {string|Array<string>=} options.pattern
*/ */
fetchSpecs (options) { fetchSpecs (options) {
this.__gunnerInstances = this.__await__.push(Promise.map(requireDeep(options), each => {
Promise.map( this.__gunnerInstances = this.compiler(this)(each);
requireDeep(options), }));
this.compiler(this),
);
return this; return this;
} }
@ -59,8 +60,10 @@ class Strategy {
* @param {Object=} options * @param {Object=} options
*/ */
run (options) { run (options) {
return Promise.map(this.__gunnerInstances, instance => const runOptions = options || this.__runTimeOptions;
instance.run(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.stack || _stringify(obj))
: obj; : obj;
const deepFlatten = arr => [].concat(
...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v)));
/* Returns true if a promise is passed */ /* Returns true if a promise is passed */
const isPromise = prom => prom && (typeof prom.then === 'function'); const isPromise = prom => prom && (typeof prom.then === 'function');
@ -40,6 +43,9 @@ module.exports = {
/* Flattens an array of arrays to an array */ /* Flattens an array of arrays to an array */
flatten : arrData => [].concat.apply([], arrData), flatten : arrData => [].concat.apply([], arrData),
/* Deep flattens arrays */
deepFlatten,
/* Maps a function over an array */ /* Maps a function over an array */
map : fn => x => x.map(fn), map : fn => x => x.map(fn),
@ -83,4 +89,7 @@ module.exports = {
/* Check if object has given property */ /* Check if object has given property */
hasProp : obj => prop => prop in obj, 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 fs = require(`fs`).promises;
const { const {
map, map,

Loading…
Cancel
Save