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

74
src/lib/runTests.js

@ -1,12 +1,40 @@
'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 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(
instance.__hooks__.before[constants.Start] || [],
[
...instance.__hooks__.before[constants.Start] || [],
...instance.__hooks__.after[constants.Start] || [],
],
hook => hook.run(),
);
@ -50,15 +78,32 @@ const runTests = instance => {
return [
state,
toTest
.then(() => ({
description: each.description,
result: constants.pass
}))
.catch(e => ({
description: each.description,
result: constants.fail,
error: e
})),
.then(() => {
log(
`${chalk`{green ✅}`} :: `,
`${each.description}`
);
return {
description: each.description,
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(
instance.__hooks__.before[constants.End] || [],
[
...(instance.__hooks__.before[constants.End] || []),
...(instance.__hooks__.after[constants.End] || []),
],
hook => hook.run(state, state['@results']),
);

Loading…
Cancel
Save