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. 27
      gunner/index.js
  2. 18
      gunner/lib/expect.js
  3. 36
      sample.test.js

27
gunner/index.js

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

18
gunner/lib/expect.js

@ -14,7 +14,7 @@ const expectPromise = (pred, statement, options = {}) => a => (...b) => {
) )
) )
.catch(e => .catch(e =>
(options.shouldNotCatch) options.shouldNotCatch
? _assertPromise( ? _assertPromise(
pred(a, ...b), pred(a, ...b),
statement(e, ...b) statement(e, ...b)
@ -29,43 +29,43 @@ const expectPromise = (pred, statement, options = {}) => a => (...b) => {
); );
}; };
const expect = a => { const expect = thing => {
return ({ return ({
done : () => Promise.resolve(), done : () => Promise.resolve(),
equal : expectPromise( equal : expectPromise(
(a, b) => a === b, (a, b) => a === b,
(a, b) => `${a} is not equal to ${b}`, (a, b) => `${a} is not equal to ${b}`,
)(a), )(thing),
deepEqual : expectPromise( deepEqual : expectPromise(
(a, b) => isEq(a, b), (a, b) => isEq(a, b),
(a, b) => `${stringify(a)} is not deeply equal to ${stringify(b)}`, (a, b) => `${stringify(a)} is not deeply equal to ${stringify(b)}`,
)(a), )(thing),
isTrue : expectPromise( isTrue : expectPromise(
a => a === true, a => a === true,
a => `${a} is not true`, a => `${a} is not true`,
)(a), )(thing),
hasProp : expectPromise( hasProp : expectPromise(
(a, b) => b in a, (a, b) => b in a,
(a, b) => `Property ${b} does not exist in ${stringify(a)}`, (a, b) => `Property ${b} does not exist in ${stringify(a)}`,
)(a), )(thing),
hasPair : expectPromise( hasPair : expectPromise(
(a, b, c) => a[b] === c, (a, b, c) => a[b] === c,
(a, b, c) => `Pair <${b}, ${c}> does not exist in ${stringify(a)}`, (a, b, c) => `Pair <${b}, ${c}> does not exist in ${stringify(a)}`,
)(a), )(thing),
resolvesTo : expectPromise( resolvesTo : expectPromise(
(a, b) => (a && typeof a.then === 'function') (a, b) => (a && typeof a.then === 'function')
? a.then(x => x === b ? Promise.resolve() : Promise.reject()) ? a.then(x => x === b ? Promise.resolve() : Promise.reject())
: Promise.reject(`${a} was not a Promise`), : Promise.reject(`${a} was not a Promise`),
(a, b) => `${a} does not resolve to ${b}`, (a, b) => `${a} does not resolve to ${b}`,
)(a), )(thing),
isPromise : expectPromise( isPromise : expectPromise(
a => (a && typeof a.then === 'function') a => (a && typeof a.then === 'function')
? a.then(() => Promise.resolve()).catch(() => Promise.resolve()) ? a.then(() => Promise.resolve()).catch(() => Promise.resolve())
: Promise.reject(), : Promise.reject(),
a => `${a} is not a Promise`, a => `${a} is not a Promise`,
{ shouldNotCatch: true }, { 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; const a = 1;
gunner.test('expression should be true', expect => expect(a === 1).isTrue()); 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