Browse Source

[events] emitter-observer event model

master
Muthu Kumar 6 years ago
parent
commit
6360305b28
  1. 2
      package.json
  2. 2
      sample/sample7.test.js
  3. 18
      src/gunner.js
  4. 5
      src/lib/emitter.js
  5. 42
      src/reporters/default.js

2
package.json

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

2
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 trace = process.argv.slice(2).indexOf('--trace') !== -1;
const log = process.argv.slice(2).indexOf('--log') !== -1; const log = process.argv.slice(2).indexOf('--log') !== -1;
gunner.run({ trace, log }).then(console.log); gunner.run({ trace, log });

18
src/gunner.js

@ -1,8 +1,10 @@
'use strict'; 'use strict';
const { arrayOrPush } = require('./util'); 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 testrunner = require('./lib/testrunner');
const { expect, expectMany } = require('./lib/expect'); const { expect, expectMany } = require('./lib/expect');
@ -67,15 +69,21 @@ class Gunner {
} }
run (options = {}) { run (options = {}) {
(options.reporter || reporter)(emitter, options);
emitter.emit('start');
return testrunner(this, options) return testrunner(this, options)
.then(results => { .then(results => {
const success = results.filter(r => r.status === 'ok'); results.success = results.filter(r => r.status === 'ok');
const successPercent = Math.floor( results.successPercent = Math.floor(
success.length/results.length * 100 results.success.length/results.length * 100
); );
if((successPercent !== 100) && typeof process !== 'undefined') if((results.successPercent !== 100)
&& typeof process !== 'undefined')
process.exitCode = 1; process.exitCode = 1;
emitter.emit('test end', results);
emitter.emit('end', results);
return results; return results;
}); });

5
src/lib/emitter.js

@ -0,0 +1,5 @@
const EventEmitter = require('events');
class GunnerEmitter extends EventEmitter {}
module.exports = new GunnerEmitter();

42
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;
Loading…
Cancel
Save