Browse Source

[error-handling] Now catches errors thrown in expect callback

0.7.0-breaking-rewrite
Muthu Kumar 6 years ago
parent
commit
e5cc74c1e7
  1. 35
      gunner/index.js
  2. 18
      gunner/lib/expect.js
  3. 36
      sample.test.js

35
gunner/index.js

@ -3,7 +3,7 @@ const { log } = console;
const _runTests = require('./lib/runTests');
const _expect = require('./lib/expect');
const { stringify } = require('../util/helpers');
const { stringify, hasProp } = require('../util/helpers');
class Gunner {
@ -14,32 +14,39 @@ class Gunner {
test (description, test) {
this.tests.push({
description,
test: () => test(_expect),
test: () => {
try {
return test(_expect);
} catch (e) {
// If errors are thrown, reject them
return Promise.reject(e);
}
},
});
}
run (options = {}) {
process.stdout.write(`Running ${this.tests.length} tests...`);
const shouldLog = (hasProp(options)('log') && options.log) || !(hasProp(options)('log'));
if (shouldLog) process.stdout.write(`Running ${this.tests.length} tests...`);
return _runTests(this.tests)
.then(results => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
const success = results.filter(r => r.result === 'pass');
const successPercent = Math.floor(success.length/results.length * 100);
if (('log' in options && options.log) || !('log' in options)) {
if (shouldLog) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
const success = results.filter(r => r.result === 'pass');
const successPercent = Math.floor(success.length/results.length * 100);
log(
`\n${success.length} tests passed of ${results.length}`,
`[${successPercent}% success]\n`
);
results.forEach(r => {
const trace = (options.trace && r.error)
? `\n Traceback:\n ${stringify(r.error)}`
: '';
log(`${r.result === 'pass' ? '✅' : '❌'} ::`,
`${r.description}`,
`${options.stack ?
r.error
? `\n Traceback:\n ${stringify(r.error)}`
: ''
: ''}`);
`${trace}`);
});
}

18
gunner/lib/expect.js

@ -14,7 +14,7 @@ const expectPromise = (pred, statement, options = {}) => a => (...b) => {
)
)
.catch(e =>
(options.shouldNotCatch)
options.shouldNotCatch
? _assertPromise(
pred(a, ...b),
statement(e, ...b)
@ -29,43 +29,43 @@ const expectPromise = (pred, statement, options = {}) => a => (...b) => {
);
};
const expect = a => {
const expect = thing => {
return ({
done : () => Promise.resolve(),
equal : expectPromise(
(a, b) => a === b,
(a, b) => `${a} is not equal to ${b}`,
)(a),
)(thing),
deepEqual : expectPromise(
(a, b) => isEq(a, b),
(a, b) => `${stringify(a)} is not deeply equal to ${stringify(b)}`,
)(a),
)(thing),
isTrue : expectPromise(
a => a === true,
a => `${a} is not true`,
)(a),
)(thing),
hasProp : expectPromise(
(a, b) => b in a,
(a, b) => `Property ${b} does not exist in ${stringify(a)}`,
)(a),
)(thing),
hasPair : expectPromise(
(a, b, c) => a[b] === c,
(a, b, c) => `Pair <${b}, ${c}> does not exist in ${stringify(a)}`,
)(a),
)(thing),
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),
)(thing),
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),
)(thing),
});
};

36
sample.test.js

@ -8,6 +8,38 @@ gunner.test(`objects aren't deeply equal`, expect => expect({a : 1}).deepEqual({
const a = 1;
gunner.test('expression should be true', expect => expect(a === 1).isTrue());
gunner.test('promise must reject', expect => expect(Promise.reject(new Error('no'))).equal('rejection'));
gunner.test('promise must reject', expect =>
expect(Promise.reject(new Error('Promise Rejected'))).equal('no rejection'));
gunner.run();
gunner.test('multiple expect', expect => {
const a = { };
a.b = 1;
a.c = 2;
return [
expect(a).hasProp('b'),
expect(a).hasPair('c', 3),
];
});
const flamethrower = require('./throwingFunc');
gunner.test('should catch error', expect => {
return expect(flamethrower()).equal(5);
});
gunner.test('should be a Promise (resolved)', expect =>
expect(Promise.resolve()).isPromise());
gunner.test('should be a Promise (rejected)', expect =>
expect(Promise.reject()).isPromise());
gunner.test('should resolve to 5', expect =>
expect(Promise.resolve(5)).resolvesTo(5));
gunner.test('should not resolve to 5', expect =>
expect(Promise.resolve()).resolvesTo(5));
gunner.run({ trace: false });

Loading…
Cancel
Save