diff --git a/gunner/lib/runTests.js b/gunner/lib/runTests.js index 053f8a3..3d65991 100644 --- a/gunner/lib/runTests.js +++ b/gunner/lib/runTests.js @@ -1,12 +1,30 @@ 'use strict'; +const { isPromise } = require('../../util/helpers'); + const { pass, fail } = require('./constants'); const runTests = tests => Promise.all(tests.map(each => { + const pred = each.test(); - return (Array.isArray(pred) ? Promise.all(pred) : pred) + + /* There are 4 different cases at play: + 1. A plain expect() is returned. + 2. An array of [ expect() ] is returned + 3. A plain expect() is wrapped in a promise + 4. An array of [ expect() ] is wrapped in a promise. + Here we normalise all of them into something we can process */ + + if (!isPromise(pred) && !(pred && isPromise(pred[0]))) + throw new Error(`Malformed test '${each.description}'`); + const toTest = Array.isArray(pred) + ? Promise.all(pred) + : pred.then(x => Array.isArray(x) ? Promise.all(x) : x); + + return toTest .then(() => ({ description: each.description, result: pass })) .catch(e => ({ description: each.description, result: fail, error: e })); + })); module.exports = runTests; diff --git a/util/helpers.js b/util/helpers.js index 88270f9..16ad2d9 100644 --- a/util/helpers.js +++ b/util/helpers.js @@ -1,5 +1,8 @@ module.exports = { + /* Returns true if a promise is passed */ + isPromise : prom => prom && (typeof prom.then === 'function'), + /* Flattens an array of arrays to an array */ flatten : arrData => [].concat.apply([], arrData),