Browse Source

[refactor]

0.7.0-breaking-rewrite
Muthu Kumar 7 years ago
parent
commit
d98bf76901
  1. 62
      gunner/lib/expect.js
  2. 52
      sample.test.js

62
gunner/lib/expect.js

@ -1,33 +1,43 @@
const isEq = require('@codefeathers/iseq'); const isEq = require('@codefeathers/iseq');
const { stringify } = require('../../util/helpers'); const { stringify, liftPromise, isPromise } = require('../../util/helpers');
const _assertPromise = require('./assertPromise'); const _assertPromise = require('./assertPromise');
const expectPromise = (pred, statement, options = {}) => a => (...b) => { const assertPromise = (
if(a && typeof a.then === 'function') { pred,
return ( statement,
a value,
.then(x => original,
_assertPromise( ...testValues
pred(a, ...b), ) => _assertPromise(
statement(x, ...b) pred(original, ...testValues),
) statement(value, ...testValues)
) );
.catch(e =>
options.shouldNotCatch const expectPromise = (pred, statement, options = {}) =>
? _assertPromise( toTest =>
pred(a, ...b), (...testValues) =>
statement(e, ...b) liftPromise(
x => assertPromise(
pred,
statement,
x,
toTest,
...testValues
),
toTest
) )
: Promise.reject(e) .catch(rejectedValue =>
options.shouldCatch
? assertPromise(
pred,
statement,
rejectedValue,
toTest,
...testValues
) )
: Promise.reject(rejectedValue)
); );
}
return _assertPromise(
pred(a, ...b),
statement(a, ...b)
);
};
const expect = thing => { const expect = thing => {
@ -54,17 +64,17 @@ const expect = thing => {
(a, b, c) => `Pair <${b}, ${c}> does not exist in ${stringify(a)}`, (a, b, c) => `Pair <${b}, ${c}> does not exist in ${stringify(a)}`,
)(thing), )(thing),
resolvesTo : expectPromise( resolvesTo : expectPromise(
(a, b) => (a && typeof a.then === 'function') (a, b) => isPromise(a)
? 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}`,
)(thing), )(thing),
isPromise : expectPromise( isPromise : expectPromise(
a => (a && typeof a.then === 'function') a => isPromise(a)
? 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 }, { shouldCatch: true },
)(thing), )(thing),
}); });

52
sample.test.js

@ -5,12 +5,32 @@ const a = 1;
gunner.test('should automatically pass', expect => expect().done()); gunner.test('should automatically pass', expect => expect().done());
gunner.test(`should be equal`, expect => expect(1).equal(1)); 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 are deep equal`, expect => expect({ a: 1 }).deepEqual({ a: 1 }));
gunner.test(`objects aren't deeply equal`, expect => expect({a : 1}).deepEqual({ a: 2 }));
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 =>
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('file must have hello as content', async expect => {
const { readFile } = require('fs').promises;
const file = await readFile('./hello.txt', { encoding: 'utf8' });
return [
expect(file).equal('hello'),
expect(file.length).equal(5),
];
});
gunner.test(`(should fail) objects aren't deeply equal`, expect => expect({a : 1}).deepEqual({ a: 2 }));
gunner.test('(should fail) promise must reject', expect =>
expect(Promise.reject(new Error('Promise Rejected'))).equal('no rejection')); expect(Promise.reject(new Error('Promise Rejected'))).equal('no rejection'));
gunner.test('multiple expect', expect => { gunner.test('(should fail) multiple expect', expect => {
const a = { }; const a = { };
a.b = 1; a.b = 1;
@ -27,30 +47,12 @@ const flamethrower = () => {
throw new Error('This burns!'); throw new Error('This burns!');
}; };
gunner.test('should catch error', expect => { gunner.test('(should fail) should catch error', expect => {
return expect(flamethrower()).equal(5); return expect(flamethrower()).equal(5);
}); });
gunner.test('should be a Promise (resolved)', expect => gunner.test('(should fail) should not resolve to 5', expect =>
expect(Promise.resolve()).isPromise()); expect(Promise.resolve({})).resolvesTo(5));
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.test('file must have hello as content', async expect => {
const { readFile } = require('fs').promises;
const file = await readFile('./hello.txt', { encoding: 'utf8' });
return [
expect(file).equal('hello'),
expect(file.length).equal(5),
];
});
gunner.run({ trace: false }); gunner.run({ trace: true});

Loading…
Cancel
Save