From 6360305b28f205ded2bf7742b2374ee57f34e16b Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Fri, 14 Sep 2018 10:28:51 +0530 Subject: [PATCH] [events] emitter-observer event model --- package.json | 2 +- sample/sample7.test.js | 2 +- src/gunner.js | 18 +++++++++++++----- src/lib/emitter.js | 5 +++++ src/reporters/default.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 src/lib/emitter.js create mode 100644 src/reporters/default.js diff --git a/package.json b/package.json index 22bf02b..21ec63e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@klenty/gunner", - "version": "0.8.7", + "version": "0.9.0", "description": "Zero magic, fast test-runner and assertion framework. No magic globals.", "main": "index.js", "repository": { diff --git a/sample/sample7.test.js b/sample/sample7.test.js index da4d5ff..091e1ff 100644 --- a/sample/sample7.test.js +++ b/sample/sample7.test.js @@ -99,4 +99,4 @@ gunner.test('(should fail) should not resolve to 5', () => const trace = process.argv.slice(2).indexOf('--trace') !== -1; const log = process.argv.slice(2).indexOf('--log') !== -1; -gunner.run({ trace, log }).then(console.log); +gunner.run({ trace, log }); diff --git a/src/gunner.js b/src/gunner.js index 2470336..7510725 100644 --- a/src/gunner.js +++ b/src/gunner.js @@ -1,8 +1,10 @@ 'use strict'; const { arrayOrPush } = require('./util'); -const caller = require('./lib/caller'); +const caller = require('./lib/caller'); +const emitter = require('./lib/emitter'); +const reporter = require('./reporters/default'); const testrunner = require('./lib/testrunner'); const { expect, expectMany } = require('./lib/expect'); @@ -67,15 +69,21 @@ class Gunner { } run (options = {}) { + (options.reporter || reporter)(emitter, options); + + emitter.emit('start'); return testrunner(this, options) .then(results => { - const success = results.filter(r => r.status === 'ok'); - const successPercent = Math.floor( - success.length/results.length * 100 + results.success = results.filter(r => r.status === 'ok'); + results.successPercent = Math.floor( + results.success.length/results.length * 100 ); - if((successPercent !== 100) && typeof process !== 'undefined') + if((results.successPercent !== 100) + && typeof process !== 'undefined') process.exitCode = 1; + emitter.emit('test end', results); + emitter.emit('end', results); return results; }); diff --git a/src/lib/emitter.js b/src/lib/emitter.js new file mode 100644 index 0000000..65ccd9b --- /dev/null +++ b/src/lib/emitter.js @@ -0,0 +1,5 @@ +const EventEmitter = require('events'); + +class GunnerEmitter extends EventEmitter {} + +module.exports = new GunnerEmitter(); diff --git a/src/reporters/default.js b/src/reporters/default.js new file mode 100644 index 0000000..07f5667 --- /dev/null +++ b/src/reporters/default.js @@ -0,0 +1,42 @@ +const statusMap = { + + 'ok': [ 'ok', '✅' ], + 'notOk': [ 'notOk', '❌' ], + 'skip': [ 'skip', '⚠️' ], + +}; + +function Default (runner, options) { + + runner.on('start', () => console.log('Started tests')); + + runner.on('test end', results => { + + results.map(x => { + + const s = statusMap[x.status]; + + console.log('>', s[0], s[1], x.description); + (options.trace + && x.reason + && console.log('\n---\n\n', x.reason, '\n\n---\n')); + + }); + + }); + + runner.on('end', results => { + + console.log( + results.success.length, + 'tests of', + results.length, + 'passed' + ); + console.log('Success ratio:', results.successPercent, '%'); + + }); + +} + +module.exports = Default;