From e447b2f0147c46b6ab8f0bea566f51b74ec3dfba Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Fri, 3 Aug 2018 13:42:57 +0530 Subject: [PATCH] [expect] More promise specific test cases --- gunner/constants.js | 6 ---- gunner/lib/assertPromise.js | 5 ++- gunner/lib/constants.js | 6 ++++ gunner/lib/expect.js | 81 ++++++++++++++++++++++++++++++++++----------- 4 files changed, 71 insertions(+), 27 deletions(-) delete mode 100644 gunner/constants.js create mode 100644 gunner/lib/constants.js diff --git a/gunner/constants.js b/gunner/constants.js deleted file mode 100644 index dc5fb3c..0000000 --- a/gunner/constants.js +++ /dev/null @@ -1,6 +0,0 @@ -const constants = { - pass: 'pass', - fail: 'fail', -}; - -module.exports = constants; diff --git a/gunner/lib/assertPromise.js b/gunner/lib/assertPromise.js index 91ee891..6b5e0fe 100644 --- a/gunner/lib/assertPromise.js +++ b/gunner/lib/assertPromise.js @@ -1,3 +1,6 @@ -const _assertPromise = (bool, assertion) => bool ? Promise.resolve() : Promise.reject(assertion); +const _assertPromise = (bool, assertion) => { + if(bool && typeof bool.then === 'function') return bool.catch(() => Promise.reject(assertion)); + return bool ? Promise.resolve() : Promise.reject(assertion); +}; module.exports = _assertPromise; diff --git a/gunner/lib/constants.js b/gunner/lib/constants.js new file mode 100644 index 0000000..dc5fb3c --- /dev/null +++ b/gunner/lib/constants.js @@ -0,0 +1,6 @@ +const constants = { + pass: 'pass', + fail: 'fail', +}; + +module.exports = constants; diff --git a/gunner/lib/expect.js b/gunner/lib/expect.js index 30e375d..148a9f6 100644 --- a/gunner/lib/expect.js +++ b/gunner/lib/expect.js @@ -1,32 +1,73 @@ const isEq = require('@codefeathers/iseq'); -const { liftPromise, stringify } = require('../../util/helpers'); +const { stringify } = require('../../util/helpers'); const _assertPromise = require('./assertPromise'); +const expectPromise = (pred, statement, options = {}) => a => (...b) => { + if(a && typeof a.then === 'function') { + return ( + a + .then(x => + _assertPromise( + pred(a, ...b), + statement(x, ...b) + ) + ) + .catch(e => + (options.shouldNotCatch) + ? _assertPromise( + pred(a, ...b), + statement(e, ...b) + ) + : Promise.reject(e) + ) + ); + } + return _assertPromise( + pred(a, ...b), + statement(a, ...b) + ); +}; + const expect = a => { return ({ done : () => Promise.resolve(), - equal : (b) => - liftPromise( - x => _assertPromise( - x === b, - `${a} is not equal to ${b}`), - a), - deepEqual : b => - liftPromise( - x => _assertPromise( - isEq(x, b), - `${stringify(a)} is not deeply equal to ${stringify(b)}`, - a), - a), - isTrue : () => - liftPromise( - x => _assertPromise( - x === true, - `${a} is not true`), - a), + equal : expectPromise( + (a, b) => a === b, + (a, b) => `${a} is not equal to ${b}`, + )(a), + deepEqual : expectPromise( + (a, b) => isEq(a, b), + (a, b) => `${stringify(a)} is not deeply equal to ${stringify(b)}`, + )(a), + isTrue : expectPromise( + a => a === true, + a => `${a} is not true`, + )(a), + hasProp : expectPromise( + (a, b) => b in a, + (a, b) => `Property ${b} does not exist in ${stringify(a)}`, + )(a), + hasPair : expectPromise( + (a, b, c) => a[b] === c, + (a, b, c) => `Pair <${b}, ${c}> does not exist in ${stringify(a)}`, + )(a), + resolvesTo : expectPromise( + (a, b) => (a && typeof a.then === 'function') + ? a.then(x => x === b ? Promise.resolve() : Promise.reject()) + : Promise.reject(`${a} was not a Promise`), + (a, b) => `${a} does not resolve to ${b}`, + )(a), + isPromise : expectPromise( + a => (a && typeof a.then === 'function') + ? a.then(() => Promise.resolve()).catch(() => Promise.resolve()) + : Promise.reject(), + a => `${a} is not a Promise`, + { shouldNotCatch: true }, + )(a), }); + }; module.exports = expect;