Browse Source

[error-handling] Pretty printing Promise rejections

0.7.0-breaking-rewrite
Muthu Kumar 6 years ago
parent
commit
049bc164d5
  1. 6
      gunner/constants.js
  2. 56
      gunner/index.js
  3. 3
      gunner/lib/assertPromise.js
  4. 32
      gunner/lib/expect.js
  5. 11
      gunner/lib/runTests.js
  6. 19
      sample.test.js

6
gunner/constants.js

@ -0,0 +1,6 @@
const constants = {
pass: 'pass',
fail: 'fail',
};
module.exports = constants;

56
gunner/index.js

@ -1,35 +1,7 @@
const isEq = require('@codefeathers/iseq');
const constants = {
pass: 'pass',
fail: 'fail',
};
const _assert = (bool, assertion) => bool ? Promise.resolve() : Promise.reject(assertion);
const stringify = obj =>
typeof obj === 'object'
? JSON.stringify(obj)
: obj;
const be = a => {
const fn = b => _assert(a === b, `${a} is not equal to ${b}`);
fn.deepEqual = b => _assert(isEq(a, b), `${stringify(a)} is not deeply equal to ${stringify(b)}`);
fn.true = () => _assert(a === true, `${a} is not true`);
return fn;
};
const expect = thing => ({
to: {
be: be(thing),
},
});
const runTests = tests => tests.map(test =>
test.test()
.then(() => ({ description: test.description, result: constants.pass }))
.catch(e => ({ description: test.description, result: constants.fail, error: e }))
);
const _runTests = require('./lib/runTests');
const _expect = require('./lib/expect');
const { stringify } = require('../util/helpers');
class Gunner {
@ -40,14 +12,30 @@ class Gunner {
test (description, test) {
this.tests.push({
description,
test: () => test(expect),
test: () => test(_expect),
});
}
run () {
return Promise.all(runTests(this.tests));
return _runTests(this.tests)
.then(results => {
const success = results.filter(r => r.result === 'pass');
const successPercent = Math.floor(success.length/results.length * 100);
console.log(
`\n${success.length} tests passed of ${results.length}`,
`[${successPercent}% success]\n`
);
results.forEach(r => {
console.log(`${r.result === 'pass' ? '✅' : '❌'} ::`,
`${r.description}`,
`${r.error ? `\n Traceback:\n ${stringify(r.error)}` : ''}`);
});
return results;
});
}
}
module.exports = Gunner;
module.exports.assert = _expect;

3
gunner/lib/assertPromise.js

@ -0,0 +1,3 @@
const _assertPromise = (bool, assertion) => bool ? Promise.resolve() : Promise.reject(assertion);
module.exports = _assertPromise;

32
gunner/lib/expect.js

@ -0,0 +1,32 @@
const isEq = require('@codefeathers/iseq');
const { liftPromise, stringify } = require('../../util/helpers');
const _assertPromise = require('./assertPromise');
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),
});
};
module.exports = expect;

11
gunner/lib/runTests.js

@ -0,0 +1,11 @@
'use strict';
const { pass, fail } = require('../constants');
const runTests = tests => Promise.all(tests.map(test =>
test.test()
.then(() => ({ description: test.description, result: pass }))
.catch(e => ({ description: test.description, result: fail, error: e }))
));
module.exports = runTests;

19
sample.test.js

@ -1,18 +1,13 @@
const Gunner = require('./gunner');
const gunner = new Gunner();
gunner.test('should return okay', expect => expect(1).to.be(1));
gunner.test('objects are equal', expect => expect({ a: 1 }).to.be.deepEqual({ a: 1 }));
gunner.test('test should break', expect => expect({a : 1}).to.be.deepEqual({ a: 2 }));
gunner.test('should automatically pass', expect => expect().done());
gunner.test(`should be equal`, expect => expect(1).equal(1));
gunner.test(`objects are deep equal`, expect => expect({ a: 1 }).deepEqual({ a: 1 }));
gunner.test(`objects aren't deeply equal`, expect => expect({a : 1}).deepEqual({ a: 2 }));
const a = 1;
gunner.test('should be true', expect => expect(a === 1).to.be.true());
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.run().then(results => {
const success = results.filter(r => r.result === 'pass');
console.log(`\n${success.length} tests passed of ${results.length}\n`);
results.forEach(r => {
console.log(`${r.result === 'pass' ? '✅' : '❌'} :: ${r.description}${r.error ? `\n ${JSON.stringify(r.error)}` : ''}`);
});
});
gunner.run();

Loading…
Cancel
Save