Browse Source

[runner] Fixed error on async func when expect is returned in promise

0.7.0-breaking-rewrite
Muthu Kumar 7 years ago
parent
commit
8050a369c7
  1. 20
      gunner/lib/runTests.js
  2. 3
      util/helpers.js

20
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;

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

Loading…
Cancel
Save