Browse Source

[logger] added logger module

0.7.0-breaking-rewrite
Muthu Kumar 6 years ago
parent
commit
0f3e5a6c7a
  1. 2
      package.json
  2. 84
      src/lib/logger.js

2
package.json

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

84
src/lib/logger.js

@ -1,12 +1,92 @@
const chalk = require('chalk');
const { taggedStringify: _ } = require('../util');
const { EOL, greenLine, redLine } = require('../util/constants');
const processResults = (results, options) => {
const success = results.filter(r => r.result === 'pass');
results.passing = success.length;
const successPercent = Math.floor(
success.length/results.length * 100
);
let resultString = '';
resultString +=
chalk`\n{green ${success.length} }`
+ `tests passed of ${results.length} `
+ `[${successPercent}% success]\n`
+ EOL;
results.forEach(r => {
const trace = (options.trace && r.error)
? _`\n Traceback:\n ${r.error}`
: '';
resultString +=
`${r.result === 'pass'
? chalk`{green ✅}`
: chalk`{red ❌}`} :: `
+ `${r.description}`
+ `${trace}`;
});
return resultString;
};
/**
* Creates a Logger that only logs if 'log' option was true
* @param {Object} options
* @param {boolean=} options.log
* @param {boolean=} shouldProcess
*/
const logger = options => {
const log = (...strings) => options.log && console.log(...strings);
const logger = (options = {}, shouldProcess) => {
const log = (...items) => {
if (!options.log) return;
let logEmit;
if (typeof options.log === 'function') {
logEmit = (...x) => {
console.log(...x);
options.log(...x);
};
}
else logEmit = console.log;
if (!shouldProcess)
logEmit(...items);
else {
const [ results ] = items;
const success = results.filter(r => r.result === 'pass');
const successPercent = Math.floor(
success.length/results.length * 100
);
logEmit(processResults(results, options));
const demarkerLine =
successPercent === 100
? greenLine
: redLine;
logEmit(
EOL,
demarkerLine,
EOL, EOL,
chalk.green(` ${success.length}`),
`tests passed of ${results.length}`,
`[${successPercent}% success]`,
EOL, EOL,
demarkerLine
);
}
};
log.true = options.log;
return log;
};
module.exports = {

Loading…
Cancel
Save