Browse Source

[expect] More promise specific test cases

0.7.0-breaking-rewrite
Muthu Kumar 6 years ago
parent
commit
e447b2f014
  1. 5
      gunner/lib/assertPromise.js
  2. 0
      gunner/lib/constants.js
  3. 81
      gunner/lib/expect.js

5
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; module.exports = _assertPromise;

0
gunner/constants.js → gunner/lib/constants.js

81
gunner/lib/expect.js

@ -1,32 +1,73 @@
const isEq = require('@codefeathers/iseq'); const isEq = require('@codefeathers/iseq');
const { liftPromise, stringify } = require('../../util/helpers'); const { stringify } = require('../../util/helpers');
const _assertPromise = require('./assertPromise'); 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 => { const expect = a => {
return ({ return ({
done : () => Promise.resolve(), done : () => Promise.resolve(),
equal : (b) => equal : expectPromise(
liftPromise( (a, b) => a === b,
x => _assertPromise( (a, b) => `${a} is not equal to ${b}`,
x === b, )(a),
`${a} is not equal to ${b}`), deepEqual : expectPromise(
a), (a, b) => isEq(a, b),
deepEqual : b => (a, b) => `${stringify(a)} is not deeply equal to ${stringify(b)}`,
liftPromise( )(a),
x => _assertPromise( isTrue : expectPromise(
isEq(x, b), a => a === true,
`${stringify(a)} is not deeply equal to ${stringify(b)}`, a => `${a} is not true`,
a), )(a),
a), hasProp : expectPromise(
isTrue : () => (a, b) => b in a,
liftPromise( (a, b) => `Property ${b} does not exist in ${stringify(a)}`,
x => _assertPromise( )(a),
x === true, hasPair : expectPromise(
`${a} is not true`), (a, b, c) => a[b] === c,
a), (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; module.exports = expect;

Loading…
Cancel
Save