diff --git a/package.json b/package.json index c0fd850..0e96ba2 100644 --- a/package.json +++ b/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": { diff --git a/src/lib/logger.js b/src/lib/logger.js index 599981d..d0fde1e 100644 --- a/src/lib/logger.js +++ b/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 = {