Browse Source

[runtests] Added unitReducer

0.7.0-breaking-rewrite
Muthu Kumar 6 years ago
parent
commit
9051ebd8c2
  1. 2
      package.json
  2. 67
      src/lib/runTests.js

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "@klenty/gunner", "name": "@klenty/gunner",
"version": "0.6.6", "version": "0.6.7",
"description": "Zero magic, fast test-runner and assertion framework. No magic globals.", "description": "Zero magic, fast test-runner and assertion framework. No magic globals.",
"main": "index.js", "main": "index.js",
"repository": { "repository": {

67
src/lib/runTests.js

@ -1,5 +1,7 @@
'use strict'; 'use strict';
const Promise = require('bluebird');
Promise.object = require('@codefeathers/promise.object');
const chalk = require('chalk'); const chalk = require('chalk');
const logger = require('./logger'); 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 runTests = (instance, options) => {
const log = logger.create(options); const log = logger.create(options);
const beforeAll = () => Promise.map( const beforeAll = () =>
unitReducer(
[ [
...instance.__hooks__.before[constants.Start] || [], ...(instance.__hooks__.before[constants.Start] || []),
...instance.__hooks__.after[constants.Start] || [], ...(instance.__hooks__.after[constants.Start] || []),
], ],
hook => hook.run(), '@start',
); )();
const beforeEvery = state => Promise.mapSeries( const beforeEvery = state =>
instance.__hooks__.before['*'] || [], unitReducer(
hook => hook.run(state), instance.__hooks__.before['*'],
); '@every',
)(state);
const runner = state => Promise.mapSeries(instance.__tests__, each => { const runner = state => Promise.mapSeries(instance.__tests__, each => {
const beforeThis = state => Promise.mapSeries( const beforeThis =
instance.__hooks__.before[each.description] || [], unitReducer(
hook => hook.run(state), instance.__hooks__.before[each.description],
'@this'
); );
const afterThis = state => Promise.mapSeries( const afterThis =
instance.__hooks__.after[each.description] || [], unitReducer(
hook => hook.run(state), instance.__hooks__.after[each.description],
'@afterThis'
); );
return beforeEvery(state) return Promise.object({ ...state, '@every': beforeEvery(state) })
.then(newState => ({ ...state, '@every': newState })) .then(state => Promise.object({ ...state, '@this': beforeThis(state) }))
.then(state => Promise.object({ ...state, '@this': beforeThis() }))
.then(state => { .then(state => {
const pred = each.test(state); const pred = each.test(state);
@ -75,7 +95,7 @@ const runTests = (instance, options) => {
? Promise.all(pred) ? Promise.all(pred)
: pred.then(x => Array.isArray(x) ? Promise.all(x) : x); : pred.then(x => Array.isArray(x) ? Promise.all(x) : x);
return [ return ([
state, state,
toTest toTest
.then(() => { .then(() => {
@ -104,19 +124,20 @@ const runTests = (instance, options) => {
error, error,
}; };
}), }),
]; ]);
}) })
.spread((state, result) => afterThis(state).then(() => result)); .then(([state, result]) => afterThis(state).then(() => result));
}); });
const afterAll = state => Promise.mapSeries( const afterAll =
unitReducer(
[ [
...(instance.__hooks__.before[constants.End] || []), ...(instance.__hooks__.before[constants.End] || []),
...(instance.__hooks__.after[constants.End] || []), ...(instance.__hooks__.after[constants.End] || []),
], ],
hook => hook.run(state, state['@results']), '@after-all',
); );
return Promise.object({ '@start': beforeAll() }) return Promise.object({ '@start': beforeAll() })

Loading…
Cancel
Save