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. 69
      src/gunner.js
  2. 74
      src/lib/runTests.js

69
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'); const successPercent = Math.floor(
results.passing = success.length; success.length/results.length * 100
const successPercent = Math.floor( );
success.length/results.length * 100
); const beforeAfterLine =
log( successPercent === 100
chalk`\n{green ${success.length}}`, ? chalk`{green ------------------------------------}`
`tests passed of ${results.length}`, : chalk`{red ------------------------------------}`;
`[${successPercent}% success]\n`
); const log = logger.create(options);
results.forEach(r => { log(
const trace = (options.trace && r.error) EOL,
? `\n Traceback:\n ${stringify(r.error)}` beforeAfterLine,
: ''; EOL, EOL,
chalk`{green ${success.length}}`,
log( `tests passed of ${results.length}`,
`${r.result === 'pass' `[${successPercent}% success]`,
? chalk`{green ✅}` EOL, EOL,
: chalk`{red ❌}`} ::`, beforeAfterLine
`${r.description}`, );
`${trace}`
); if((successPercent !== 100) && typeof process !== 'undefined')
}); process.exitCode = 1;
}
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;
}); });
} }

74
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(() => {
description: each.description, log(
result: constants.pass `${chalk`{green ✅}`} :: `,
})) `${each.description}`
.catch(e => ({ );
description: each.description, return {
result: constants.fail, description: each.description,
error: e result: constants.pass
})), };
})
.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,
result: constants.fail,
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