Browse Source

[sample] Minor fixes and updated samples

master
Muthu Kumar 6 years ago
parent
commit
eeab27dd24
  1. 2
      package.json
  2. 98
      sample/sample.test.js
  3. 34
      sample/sample2.test.js
  4. 2
      src/gunner.js
  5. 2
      src/lib/caller.js
  6. 2
      src/lib/expect.js

2
package.json

@ -1,6 +1,6 @@
{
"name": "@klenty/gunner",
"version": "0.8.5",
"version": "0.8.6",
"description": "Zero magic, fast test-runner and assertion framework. No magic globals.",
"main": "index.js",
"repository": {

98
sample/sample.test.js

@ -1,98 +0,0 @@
/**
* This file contains random tests
* used during development
*/
const Gunner = require('../index.js');
const gunner = new Gunner({ name: 'sample tests' });
const a = 1;
// gunner.before(Gunner.Start, () => console.log('Started tests!'));
// gunner.before(Gunner.End, () => console.log('Ended tests!'));
// let runCount = 1;
// gunner.before('*', () => console.log(`Running test ${runCount++}`));
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('expression should be true', expect => expect(a === 1).isTrue());
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('wait and resolve', () => {
return new Promise(r => {
setTimeout(
() => r('ok'),
500
);
});
});
gunner.test('should resolve to 5', expect =>
expect(Promise.resolve(5)).resolvesTo(5));
// gunner.before(
// 'file must have hello as content',
// () => console.log('>> starting test! file must have hello as content'),
// );
// gunner.after(
// 'file must have hello as content',
// () => console.log('>> finished test! file must have hello as content'),
// );
gunner.test('file must have hello as content', async expect => {
const { readFile } = require('fs').promises;
const file = await readFile(__dirname + '/hello.txt', { encoding: 'utf8' });
return [
expect(file).equal('hello'),
expect(file.length).equal(5),
];
});
gunner.test('(should fail) Should automatically fail', expect =>
expect().fail());
gunner.test('(should fail) Value is not a Promise', expect =>
expect(5).isPromise());
gunner.test('(should fail) Error is not a Promise', expect =>
expect(flamethrower()).isPromise());
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'));
gunner.test('(should fail) multiple expect', expect => {
const a = { };
a.b = 1;
a.c = 2;
return [
expect(a).hasProp('b'),
expect(a).hasPair('c', 3),
];
});
const flamethrower = () => {
throw new Error('This burns!');
};
gunner.test('(should fail) should catch error', expect => {
return expect(flamethrower()).equal(5);
});
gunner.test('(should fail) should not resolve to 5', expect =>
expect(Promise.resolve()).resolvesTo(5));
const trace = process.argv.slice(2).indexOf('--trace') !== -1;
const log = process.argv.slice(2).indexOf('--log') !== -1;
gunner.run({ trace, log });

34
sample/sample2.test.js

@ -3,23 +3,27 @@
* used during development
*/
const Gunner = require('../index.js');
const { Gunner, expect, expectMany } = require('..');
const gunner = new Gunner({ name: 'state tests' });
gunner.before(Gunner.Start, () => 'hello');
gunner.before(Gunner.Start, () => 'below');
gunner.before(Gunner.Start, () => 'shallow');
gunner.before('*', () => 'stars');
gunner.before('Test 1', () => 'nope');
gunner.before(Gunner.Start, () => 'world', 'hello');
gunner.before(Gunner.Start, () => 'earth', 'below');
gunner.before(Gunner.Start, () => 'waters', 'shallow');
gunner.before('*', () => 'stars', 'stars');
gunner.before('Test 1', () => 'nope', 'test1');
gunner.test('Test 1', (expect, state) =>
[
expect(state['@start']).deepEquals([ 'hello', 'below', 'shallow' ]),
expect(state['@every']).deepEquals([ 'stars' ]),
expect(state['@this']).deepEquals([ 'nope' ]),
]);
gunner.test('Test 1', state =>
expectMany([
expect(state['@start']).deepEquals({
hello: 'world',
below: 'earth',
shallow: 'waters'
}),
expect(state['@every']).deepEquals({ stars: 'stars' }),
expect(state['@this']).deepEquals({ test1: 'nope' }),
]));
gunner.test('(should fail) Test 2', (expect, state) =>
expect(state['@start']).deepEquals([ 'hellno' ]));
gunner.test('(should fail) Test 2', state =>
expect(state['@start']).deepEquals({ 'hellna': true }));
gunner.run({ log: true });
gunner.run({ log: true }).then(console.log);

2
src/gunner.js

@ -1,7 +1,7 @@
'use strict';
const { arrayOrPush } = require('./util');
const caller = require('./lib/unit/caller');
const caller = require('./lib/caller');
const testrunner = require('./lib/testrunner');
const { expect, expectMany } = require('./lib/expect');

2
src/lib/caller.js

@ -1,4 +1,4 @@
const { isPromise } = require('../../util');
const { isPromise } = require('../util');
const caller = (test, state) => {

2
src/lib/expect.js

@ -58,7 +58,5 @@ const expect = (thing, args) =>
const expectMany = Promise.all.bind(Promise);
expect(5).notEquals(5).then(console.log).catch(console.log);
module.exports.expect = expect;
module.exports.expectMany = expectMany;

Loading…
Cancel
Save