Browse Source

[refactor] Moved logging to runTests - logs after each test

0.7.0-breaking-rewrite
Muthu Kumar 6 years ago
parent
commit
b74c73b314
  1. 53
      src/gunner.js
  2. 66
      src/lib/runTests.js

53
src/gunner.js

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { log } = console; const { EOL } = require('os');
const chalk = require('chalk'); const chalk = require('chalk');
Promise = require('bluebird'); Promise = require('bluebird');
@ -8,8 +8,9 @@ Promise.object = require('@codefeathers/promise.object');
const _runTests = require('./lib/runTests'); const _runTests = require('./lib/runTests');
const _expect = require('./lib/expect'); const _expect = require('./lib/expect');
const logger = require('./lib/logger');
const { stringify, hasProp } = require('./util'); const { hasProp } = require('./util');
const symbols = require('./util/symbols'); const symbols = require('./util/symbols');
class Gunner { class Gunner {
@ -80,47 +81,39 @@ class Gunner {
} }
run (options = {}) { run (options = {}) {
const shouldLog = ( options.log = (options || {})['log'] || !(hasProp(options)('log'));
(hasProp(options)('log') return _runTests(this, options)
&& options.log)
|| !(hasProp(options)('log'))
);
return _runTests(this)
.then(results => { .then(results => {
if (shouldLog) {
const success = results.filter(r => r.result === 'pass'); const success = results.filter(r => r.result === 'pass');
results.passing = success.length;
const successPercent = Math.floor( const successPercent = Math.floor(
success.length/results.length * 100 success.length/results.length * 100
); );
const beforeAfterLine =
successPercent === 100
? chalk`{green ------------------------------------}`
: chalk`{red ------------------------------------}`;
const log = logger.create(options);
log( log(
chalk`\n{green ${success.length}}`, EOL,
beforeAfterLine,
EOL, EOL,
chalk`{green ${success.length}}`,
`tests passed of ${results.length}`, `tests passed of ${results.length}`,
`[${successPercent}% success]\n` `[${successPercent}% success]`,
EOL, EOL,
beforeAfterLine
); );
results.forEach(r => {
const trace = (options.trace && r.error)
? `\n Traceback:\n ${stringify(r.error)}`
: '';
log( if((successPercent !== 100) && typeof process !== 'undefined')
`${r.result === 'pass' process.exitCode = 1;
? chalk`{green ✅}`
: chalk`{red ❌}`} ::`,
`${r.description}`,
`${trace}`
);
});
}
return results; return results;
}) })
.then(results => { .then(results => {
if (options.exit) { if (options.exit && typeof process !== 'undefined')
if(results.passing < results.length) process.exit();
process.exit(1);
process.exit(0);
}
return results; return results;
}); });
} }

66
src/lib/runTests.js

@ -1,12 +1,40 @@
'use strict'; 'use strict';
const { isPromise } = require('../util'); const chalk = require('chalk');
const logger = require('./logger');
const { isPromise, taggedStringify: _ } = require('../util');
const constants = require('../util/symbols'); const constants = require('../util/symbols');
const runTests = instance => { const snipStack = e => {
if (e.stack)
e.stack = e.stack
.split('\n')
.reduceRight(
(acc, x) =>
/* eslint-disable-next-line */
acc.done
? acc.cur
: x.match(/at Object\.test.*\/src\/gunner\.js/)
? { cur: x, done: true }
: { cur: [x, acc.cur].join('\n') },
{ cur: '' })
.cur.trim();
return e;
};
const runTests = (instance, options) => {
const log = logger.create(options);
const beforeAll = () => Promise.map( const beforeAll = () => Promise.map(
instance.__hooks__.before[constants.Start] || [], [
...instance.__hooks__.before[constants.Start] || [],
...instance.__hooks__.after[constants.Start] || [],
],
hook => hook.run(), hook => hook.run(),
); );
@ -50,15 +78,32 @@ const runTests = instance => {
return [ return [
state, state,
toTest toTest
.then(() => ({ .then(() => {
log(
`${chalk`{green ✅}`} :: `,
`${each.description}`
);
return {
description: each.description, description: each.description,
result: constants.pass result: constants.pass
})) };
.catch(e => ({ })
.catch(e => {
const error = (e && e.stack) ? snipStack(e) : e;
const trace = (options.trace && error)
? `\n Traceback:\n ` + _`${error}`
: '';
log(
`${chalk`{red ❌}`} :: `,
`${each.description}`,
`${trace}`
);
return {
description: each.description, description: each.description,
result: constants.fail, result: constants.fail,
error: e error,
})), };
}),
]; ];
}) })
@ -67,7 +112,10 @@ const runTests = instance => {
}); });
const afterAll = state => Promise.mapSeries( const afterAll = state => Promise.mapSeries(
instance.__hooks__.before[constants.End] || [], [
...(instance.__hooks__.before[constants.End] || []),
...(instance.__hooks__.after[constants.End] || []),
],
hook => hook.run(state, state['@results']), hook => hook.run(state, state['@results']),
); );

Loading…
Cancel
Save