From eeab27dd24bf1d4fb76afa4622da8d8cd213577e Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Fri, 14 Sep 2018 09:11:36 +0530 Subject: [PATCH] [sample] Minor fixes and updated samples --- package.json | 2 +- sample/sample.test.js | 98 -------------------------------------------------- sample/sample2.test.js | 34 ++++++++++-------- src/gunner.js | 2 +- src/lib/caller.js | 2 +- src/lib/expect.js | 2 -- 6 files changed, 22 insertions(+), 118 deletions(-) delete mode 100644 sample/sample.test.js diff --git a/package.json b/package.json index 8ce1e8d..de8b87e 100644 --- a/package.json +++ b/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": { diff --git a/sample/sample.test.js b/sample/sample.test.js deleted file mode 100644 index b7efe11..0000000 --- a/sample/sample.test.js +++ /dev/null @@ -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 }); diff --git a/sample/sample2.test.js b/sample/sample2.test.js index 8ffb9c2..ee5d1af 100644 --- a/sample/sample2.test.js +++ b/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 }); \ No newline at end of file +gunner.run({ log: true }).then(console.log); \ No newline at end of file diff --git a/src/gunner.js b/src/gunner.js index d52eff0..2470336 100644 --- a/src/gunner.js +++ b/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'); diff --git a/src/lib/caller.js b/src/lib/caller.js index 639e19f..012d9e3 100644 --- a/src/lib/caller.js +++ b/src/lib/caller.js @@ -1,4 +1,4 @@ -const { isPromise } = require('../../util'); +const { isPromise } = require('../util'); const caller = (test, state) => { diff --git a/src/lib/expect.js b/src/lib/expect.js index 5509a40..9d53073 100644 --- a/src/lib/expect.js +++ b/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;