You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.0 KiB
54 lines
1.0 KiB
6 years ago
|
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 }))
|
||
|
);
|
||
|
|
||
|
class Gunner {
|
||
|
|
||
|
constructor () {
|
||
|
this.tests = [];
|
||
|
}
|
||
|
|
||
|
test (description, test) {
|
||
|
this.tests.push({
|
||
|
description,
|
||
|
test: () => test(expect),
|
||
|
});
|
||
|
}
|
||
|
|
||
|
run () {
|
||
|
return Promise.all(runTests(this.tests));
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
module.exports = Gunner;
|