Skip to content

Commit 7b764ea

Browse files
jamestalmagesindresorhus
authored andcommitted
Close #412 PR: Standardize colors across reporters.. Fixes #410
1 parent 37e0dd8 commit 7b764ea

File tree

4 files changed

+44
-33
lines changed

4 files changed

+44
-33
lines changed

cli.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ var updateNotifier = require('update-notifier');
2121
var figures = require('figures');
2222
var arrify = require('arrify');
2323
var meow = require('meow');
24-
var chalk = require('chalk');
2524
var Promise = require('bluebird');
2625
var pkgConf = require('pkg-conf');
26+
var colors = require('./lib/colors');
2727
var verboseReporter = require('./lib/reporters/verbose');
2828
var miniReporter = require('./lib/reporters/mini');
2929
var tapReporter = require('./lib/reporters/tap');
@@ -111,9 +111,9 @@ api.run()
111111
})
112112
.catch(function (err) {
113113
if (err.name === 'AvaError') {
114-
console.log(' ' + chalk.red(figures.cross) + ' ' + err.message);
114+
console.log(' ' + colors.error(figures.cross) + ' ' + err.message);
115115
} else {
116-
console.error(err.stack);
116+
console.error(colors.stack(err.stack));
117117
}
118118

119119
logger.exit(1);

lib/colors.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
var chalk = require('chalk');
4+
5+
module.exports = {
6+
error: chalk.red,
7+
skip: chalk.yellow,
8+
pass: chalk.green,
9+
duration: chalk.gray.dim,
10+
stack: chalk.red
11+
};

lib/reporters/mini.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
var logUpdate = require('log-update');
3-
var chalk = require('chalk');
3+
var colors = require('../colors');
44
var plur = require('plur');
55
var beautifyStack = require('../beautify-stack');
66

@@ -28,25 +28,25 @@ MiniReporter.prototype.test = function (test) {
2828
var title;
2929

3030
if (test.skip) {
31-
title = chalk.yellow('- ' + test.title);
31+
title = colors.skip('- ' + test.title);
3232
this.skipCount++;
3333
} else if (test.error) {
34-
title = chalk.red(test.title);
34+
title = colors.error(test.title);
3535
this.failCount++;
3636
} else {
37-
title = chalk.green(test.title);
37+
title = colors.pass(test.title);
3838
this.passCount++;
3939
}
4040

4141
status += ' ' + title;
4242
status += '\n\n';
4343

4444
if (this.passCount > 0) {
45-
status += ' ' + chalk.green(this.passCount, 'passed');
45+
status += ' ' + colors.pass(this.passCount, 'passed');
4646
}
4747

4848
if (this.failCount > 0) {
49-
status += ' ' + chalk.red(this.failCount, 'failed');
49+
status += ' ' + colors.error(this.failCount, 'failed');
5050
}
5151

5252
return status;
@@ -66,23 +66,23 @@ MiniReporter.prototype.finish = function () {
6666
var status = '\n';
6767

6868
if (this.passCount > 0) {
69-
status += ' ' + chalk.green(this.passCount, 'passed');
69+
status += ' ' + colors.pass(this.passCount, 'passed');
7070
}
7171

7272
if (this.skipCount > 0) {
73-
status += ' ' + chalk.yellow(this.skipCount, 'skipped');
73+
status += ' ' + colors.skip(this.skipCount, 'skipped');
7474
}
7575

7676
if (this.failCount > 0) {
77-
status += ' ' + chalk.red(this.failCount, 'failed');
77+
status += ' ' + colors.error(this.failCount, 'failed');
7878
}
7979

8080
if (this.rejectionCount > 0) {
81-
status += '\n ' + chalk.red(this.rejectionCount, plur('rejection', this.rejectionCount));
81+
status += '\n ' + colors.error(this.rejectionCount, plur('rejection', this.rejectionCount));
8282
}
8383

8484
if (this.exceptionCount > 0) {
85-
status += '\n ' + chalk.red(this.exceptionCount, plur('exception', this.exceptionCount));
85+
status += '\n ' + colors.error(this.exceptionCount, plur('exception', this.exceptionCount));
8686
}
8787

8888
var i = 0;
@@ -105,8 +105,8 @@ MiniReporter.prototype.finish = function () {
105105
description = JSON.stringify(test);
106106
}
107107

108-
status += '\n\n ' + chalk.red(i + '.', title) + '\n';
109-
status += chalk.red(description);
108+
status += '\n\n ' + colors.error(i + '.', title) + '\n';
109+
status += colors.stack(description);
110110
});
111111
}
112112

@@ -121,8 +121,8 @@ MiniReporter.prototype.finish = function () {
121121
var title = err.type === 'rejection' ? 'Unhandled Rejection' : 'Uncaught Exception';
122122
var description = err.stack ? beautifyStack(err.stack) : JSON.stringify(err);
123123

124-
status += '\n\n ' + chalk.red(i + '.', title) + '\n';
125-
status += ' ' + chalk.red(description);
124+
status += '\n\n ' + colors.error(i + '.', title) + '\n';
125+
status += ' ' + colors.stack(description);
126126
});
127127
}
128128

lib/reporters/verbose.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
var prettyMs = require('pretty-ms');
33
var figures = require('figures');
4-
var chalk = require('chalk');
4+
var colors = require('../colors');
55
var plur = require('plur');
66
var beautifyStack = require('../beautify-stack');
77

@@ -19,11 +19,11 @@ VerboseReporter.prototype.start = function () {
1919

2020
VerboseReporter.prototype.test = function (test) {
2121
if (test.error) {
22-
return ' ' + chalk.red(figures.cross) + ' ' + test.title + ' ' + chalk.red(test.error.message);
22+
return ' ' + colors.error(figures.cross) + ' ' + test.title + ' ' + colors.error(test.error.message);
2323
}
2424

2525
if (test.skip) {
26-
return ' ' + chalk.cyan('- ' + test.title);
26+
return ' ' + colors.skip('- ' + test.title);
2727
}
2828

2929
if (this.api.fileCount === 1 && this.api.testCount === 1 && test.title === '[anonymous]') {
@@ -32,9 +32,9 @@ VerboseReporter.prototype.test = function (test) {
3232

3333
// display duration only over a threshold
3434
var threshold = 100;
35-
var duration = test.duration > threshold ? chalk.gray.dim(' (' + prettyMs(test.duration) + ')') : '';
35+
var duration = test.duration > threshold ? colors.duration(' (' + prettyMs(test.duration) + ')') : '';
3636

37-
return ' ' + chalk.green(figures.tick) + ' ' + test.title + duration;
37+
return ' ' + colors.pass(figures.tick) + ' ' + test.title + duration;
3838
};
3939

4040
VerboseReporter.prototype.unhandledError = function (err) {
@@ -43,12 +43,12 @@ VerboseReporter.prototype.unhandledError = function (err) {
4343
exception: 'Uncaught Exception'
4444
};
4545

46-
var output = chalk.red(types[err.type] + ':', err.file) + '\n';
46+
var output = colors.error(types[err.type] + ':', err.file) + '\n';
4747

4848
if (err.stack) {
49-
output += ' ' + chalk.red(beautifyStack(err.stack)) + '\n';
49+
output += ' ' + colors.stack(beautifyStack(err.stack)) + '\n';
5050
} else {
51-
output += ' ' + chalk.red(JSON.stringify(err)) + '\n';
51+
output += ' ' + colors.stack(JSON.stringify(err)) + '\n';
5252
}
5353

5454
output += '\n';
@@ -60,21 +60,21 @@ VerboseReporter.prototype.finish = function () {
6060
var output = '\n';
6161

6262
if (this.api.failCount > 0) {
63-
output += ' ' + chalk.red(this.api.failCount, plur('test', this.api.failCount), 'failed') + '\n';
63+
output += ' ' + colors.error(this.api.failCount, plur('test', this.api.failCount), 'failed') + '\n';
6464
} else {
65-
output += ' ' + chalk.green(this.api.passCount, plur('test', this.api.passCount), 'passed') + '\n';
65+
output += ' ' + colors.pass(this.api.passCount, plur('test', this.api.passCount), 'passed') + '\n';
6666
}
6767

6868
if (this.api.skipCount > 0) {
69-
output += ' ' + chalk.yellow(this.api.skipCount, plur('test', this.api.skipCount), 'skipped') + '\n';
69+
output += ' ' + colors.skip(this.api.skipCount, plur('test', this.api.skipCount), 'skipped') + '\n';
7070
}
7171

7272
if (this.api.rejectionCount > 0) {
73-
output += ' ' + chalk.red(this.api.rejectionCount, 'unhandled', plur('rejection', this.api.rejectionCount)) + '\n';
73+
output += ' ' + colors.error(this.api.rejectionCount, 'unhandled', plur('rejection', this.api.rejectionCount)) + '\n';
7474
}
7575

7676
if (this.api.exceptionCount > 0) {
77-
output += ' ' + chalk.red(this.api.exceptionCount, 'uncaught', plur('exception', this.api.exceptionCount)) + '\n';
77+
output += ' ' + colors.error(this.api.exceptionCount, 'uncaught', plur('exception', this.api.exceptionCount)) + '\n';
7878
}
7979

8080
if (this.api.failCount > 0) {
@@ -89,8 +89,8 @@ VerboseReporter.prototype.finish = function () {
8989

9090
i++;
9191

92-
output += ' ' + chalk.red(i + '.', test.title) + '\n';
93-
output += ' ' + chalk.red(beautifyStack(test.error.stack)) + '\n';
92+
output += ' ' + colors.error(i + '.', test.title) + '\n';
93+
output += ' ' + colors.stack(beautifyStack(test.error.stack)) + '\n';
9494
});
9595
}
9696

0 commit comments

Comments
 (0)