diff --git a/cli.js b/cli.js index 539931e5c..78a6521d8 100755 --- a/cli.js +++ b/cli.js @@ -21,9 +21,9 @@ var updateNotifier = require('update-notifier'); var figures = require('figures'); var arrify = require('arrify'); var meow = require('meow'); -var chalk = require('chalk'); var Promise = require('bluebird'); var pkgConf = require('pkg-conf'); +var colors = require('./lib/colors'); var verboseReporter = require('./lib/reporters/verbose'); var miniReporter = require('./lib/reporters/mini'); var tapReporter = require('./lib/reporters/tap'); @@ -111,9 +111,9 @@ api.run() }) .catch(function (err) { if (err.name === 'AvaError') { - console.log(' ' + chalk.red(figures.cross) + ' ' + err.message); + console.log(' ' + colors.error(figures.cross) + ' ' + err.message); } else { - console.error(err.stack); + console.error(colors.stack(err.stack)); } logger.exit(1); diff --git a/lib/colors.js b/lib/colors.js new file mode 100644 index 000000000..e9cc10741 --- /dev/null +++ b/lib/colors.js @@ -0,0 +1,11 @@ +'use strict'; + +var chalk = require('chalk'); + +module.exports = { + error: chalk.red, + skip: chalk.yellow, + pass: chalk.green, + duration: chalk.gray.dim, + stack: chalk.red +}; diff --git a/lib/reporters/mini.js b/lib/reporters/mini.js index d6f39dad6..9cf79eca7 100644 --- a/lib/reporters/mini.js +++ b/lib/reporters/mini.js @@ -1,6 +1,6 @@ 'use strict'; var logUpdate = require('log-update'); -var chalk = require('chalk'); +var colors = require('../colors'); var plur = require('plur'); var beautifyStack = require('../beautify-stack'); @@ -28,13 +28,13 @@ MiniReporter.prototype.test = function (test) { var title; if (test.skip) { - title = chalk.yellow('- ' + test.title); + title = colors.skip('- ' + test.title); this.skipCount++; } else if (test.error) { - title = chalk.red(test.title); + title = colors.error(test.title); this.failCount++; } else { - title = chalk.green(test.title); + title = colors.pass(test.title); this.passCount++; } @@ -42,11 +42,11 @@ MiniReporter.prototype.test = function (test) { status += '\n\n'; if (this.passCount > 0) { - status += ' ' + chalk.green(this.passCount, 'passed'); + status += ' ' + colors.pass(this.passCount, 'passed'); } if (this.failCount > 0) { - status += ' ' + chalk.red(this.failCount, 'failed'); + status += ' ' + colors.error(this.failCount, 'failed'); } return status; @@ -66,23 +66,23 @@ MiniReporter.prototype.finish = function () { var status = '\n'; if (this.passCount > 0) { - status += ' ' + chalk.green(this.passCount, 'passed'); + status += ' ' + colors.pass(this.passCount, 'passed'); } if (this.skipCount > 0) { - status += ' ' + chalk.yellow(this.skipCount, 'skipped'); + status += ' ' + colors.skip(this.skipCount, 'skipped'); } if (this.failCount > 0) { - status += ' ' + chalk.red(this.failCount, 'failed'); + status += ' ' + colors.error(this.failCount, 'failed'); } if (this.rejectionCount > 0) { - status += '\n ' + chalk.red(this.rejectionCount, plur('rejection', this.rejectionCount)); + status += '\n ' + colors.error(this.rejectionCount, plur('rejection', this.rejectionCount)); } if (this.exceptionCount > 0) { - status += '\n ' + chalk.red(this.exceptionCount, plur('exception', this.exceptionCount)); + status += '\n ' + colors.error(this.exceptionCount, plur('exception', this.exceptionCount)); } var i = 0; @@ -105,8 +105,8 @@ MiniReporter.prototype.finish = function () { description = JSON.stringify(test); } - status += '\n\n ' + chalk.red(i + '.', title) + '\n'; - status += chalk.red(description); + status += '\n\n ' + colors.error(i + '.', title) + '\n'; + status += colors.stack(description); }); } @@ -121,8 +121,8 @@ MiniReporter.prototype.finish = function () { var title = err.type === 'rejection' ? 'Unhandled Rejection' : 'Uncaught Exception'; var description = err.stack ? beautifyStack(err.stack) : JSON.stringify(err); - status += '\n\n ' + chalk.red(i + '.', title) + '\n'; - status += ' ' + chalk.red(description); + status += '\n\n ' + colors.error(i + '.', title) + '\n'; + status += ' ' + colors.stack(description); }); } diff --git a/lib/reporters/verbose.js b/lib/reporters/verbose.js index e322ac242..82351b13e 100644 --- a/lib/reporters/verbose.js +++ b/lib/reporters/verbose.js @@ -1,7 +1,7 @@ 'use strict'; var prettyMs = require('pretty-ms'); var figures = require('figures'); -var chalk = require('chalk'); +var colors = require('../colors'); var plur = require('plur'); var beautifyStack = require('../beautify-stack'); @@ -19,11 +19,11 @@ VerboseReporter.prototype.start = function () { VerboseReporter.prototype.test = function (test) { if (test.error) { - return ' ' + chalk.red(figures.cross) + ' ' + test.title + ' ' + chalk.red(test.error.message); + return ' ' + colors.error(figures.cross) + ' ' + test.title + ' ' + colors.error(test.error.message); } if (test.skip) { - return ' ' + chalk.cyan('- ' + test.title); + return ' ' + colors.skip('- ' + test.title); } if (this.api.fileCount === 1 && this.api.testCount === 1 && test.title === '[anonymous]') { @@ -32,9 +32,9 @@ VerboseReporter.prototype.test = function (test) { // display duration only over a threshold var threshold = 100; - var duration = test.duration > threshold ? chalk.gray.dim(' (' + prettyMs(test.duration) + ')') : ''; + var duration = test.duration > threshold ? colors.duration(' (' + prettyMs(test.duration) + ')') : ''; - return ' ' + chalk.green(figures.tick) + ' ' + test.title + duration; + return ' ' + colors.pass(figures.tick) + ' ' + test.title + duration; }; VerboseReporter.prototype.unhandledError = function (err) { @@ -43,12 +43,12 @@ VerboseReporter.prototype.unhandledError = function (err) { exception: 'Uncaught Exception' }; - var output = chalk.red(types[err.type] + ':', err.file) + '\n'; + var output = colors.error(types[err.type] + ':', err.file) + '\n'; if (err.stack) { - output += ' ' + chalk.red(beautifyStack(err.stack)) + '\n'; + output += ' ' + colors.stack(beautifyStack(err.stack)) + '\n'; } else { - output += ' ' + chalk.red(JSON.stringify(err)) + '\n'; + output += ' ' + colors.stack(JSON.stringify(err)) + '\n'; } output += '\n'; @@ -60,21 +60,21 @@ VerboseReporter.prototype.finish = function () { var output = '\n'; if (this.api.failCount > 0) { - output += ' ' + chalk.red(this.api.failCount, plur('test', this.api.failCount), 'failed') + '\n'; + output += ' ' + colors.error(this.api.failCount, plur('test', this.api.failCount), 'failed') + '\n'; } else { - output += ' ' + chalk.green(this.api.passCount, plur('test', this.api.passCount), 'passed') + '\n'; + output += ' ' + colors.pass(this.api.passCount, plur('test', this.api.passCount), 'passed') + '\n'; } if (this.api.skipCount > 0) { - output += ' ' + chalk.yellow(this.api.skipCount, plur('test', this.api.skipCount), 'skipped') + '\n'; + output += ' ' + colors.skip(this.api.skipCount, plur('test', this.api.skipCount), 'skipped') + '\n'; } if (this.api.rejectionCount > 0) { - output += ' ' + chalk.red(this.api.rejectionCount, 'unhandled', plur('rejection', this.api.rejectionCount)) + '\n'; + output += ' ' + colors.error(this.api.rejectionCount, 'unhandled', plur('rejection', this.api.rejectionCount)) + '\n'; } if (this.api.exceptionCount > 0) { - output += ' ' + chalk.red(this.api.exceptionCount, 'uncaught', plur('exception', this.api.exceptionCount)) + '\n'; + output += ' ' + colors.error(this.api.exceptionCount, 'uncaught', plur('exception', this.api.exceptionCount)) + '\n'; } if (this.api.failCount > 0) { @@ -89,8 +89,8 @@ VerboseReporter.prototype.finish = function () { i++; - output += ' ' + chalk.red(i + '.', test.title) + '\n'; - output += ' ' + chalk.red(beautifyStack(test.error.stack)) + '\n'; + output += ' ' + colors.error(i + '.', test.title) + '\n'; + output += ' ' + colors.stack(beautifyStack(test.error.stack)) + '\n'; }); }