Skip to content

Commit 931501a

Browse files
author
Thomas
committed
Added support for --no-color flag in verbose and mini reporters
1 parent 2623c11 commit 931501a

File tree

7 files changed

+93
-29
lines changed

7 files changed

+93
-29
lines changed

lib/cli.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ exports.run = () => {
6868
'tap',
6969
'verbose',
7070
'watch',
71-
'update-snapshots'
71+
'update-snapshots',
72+
'color'
7273
],
73-
default: conf,
74+
default: Object.assign({color: true}, conf),
7475
alias: {
7576
t: 'tap',
7677
v: 'verbose',
@@ -118,17 +119,18 @@ exports.run = () => {
118119
pkgDir,
119120
timeout: cli.flags.timeout,
120121
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0,
121-
updateSnapshots: cli.flags.updateSnapshots
122+
updateSnapshots: cli.flags.updateSnapshots,
123+
color: cli.flags.color
122124
});
123125

124126
let reporter;
125127

126128
if (cli.flags.tap && !cli.flags.watch) {
127129
reporter = new TapReporter();
128130
} else if (cli.flags.verbose || isCi) {
129-
reporter = new VerboseReporter({basePath: pkgDir});
131+
reporter = new VerboseReporter({color: cli.flags.color, basePath: pkgDir});
130132
} else {
131-
reporter = new MiniReporter({watching: cli.flags.watch, basePath: pkgDir});
133+
reporter = new MiniReporter({color: cli.flags.color, watching: cli.flags.watch, basePath: pkgDir});
132134
}
133135

134136
reporter.api = api;

lib/fork.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ module.exports = (file, opts, execArgv) => {
3636
} : false
3737
}, opts);
3838

39-
const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), [JSON.stringify(opts)], {
39+
const args = [JSON.stringify(opts), opts.color ? '--color' : '--no-color'];
40+
41+
const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, {
4042
cwd: opts.pkgDir,
4143
silent: true,
4244
env,

lib/reporters/mini.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@ function eraseLines(count) {
4242

4343
class MiniReporter {
4444
constructor(options) {
45+
this.options = Object.assign({}, options);
46+
47+
chalk.enabled = this.options.color;
48+
for (const key of Object.keys(colors)) {
49+
colors[key].enabled = this.options.color;
50+
}
51+
4552
const spinnerDef = spinners[process.platform === 'win32' ? 'line' : 'dots'];
4653
this.spinnerFrames = spinnerDef.frames.map(c => chalk.gray.dim(c));
4754
this.spinnerInterval = spinnerDef.interval;
4855

49-
this.options = Object.assign({}, options);
50-
5156
this.reset();
5257
this.stream = process.stderr;
5358
this.stringDecoder = new StringDecoder();

lib/reporters/verbose.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ const extractStack = require('../extract-stack');
1010
const codeExcerpt = require('../code-excerpt');
1111
const colors = require('../colors');
1212

13-
Object.keys(colors).forEach(key => {
14-
colors[key].enabled = true;
15-
});
16-
1713
class VerboseReporter {
1814
constructor(options) {
1915
this.options = Object.assign({}, options);
16+
17+
chalk.enabled = options.color;
18+
for (const key of Object.keys(colors)) {
19+
colors[key].enabled = options.color;
20+
}
2021
}
2122
start() {
2223
return '';

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ $ ava --help
163163
--verbose, -v Enable verbose output
164164
--no-cache Disable the transpiler cache
165165
--no-power-assert Disable Power Assert
166+
--no-color Disable color output
166167
--match, -m Only run tests with matching title (Can be repeated)
167168
--watch, -w Re-run tests when tests and source files change
168169
--timeout, -T Set global timeout

test/reporters/mini.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ process.stdout.columns = 5000;
2727
const fullWidthLine = chalk.gray.dim('\u2500'.repeat(5000));
2828

2929
function miniReporter(options) {
30+
if (options === undefined) {
31+
options = {color: true};
32+
}
3033
const reporter = new MiniReporter(options);
3134
reporter.start = () => '';
3235
return reporter;
@@ -35,7 +38,7 @@ function miniReporter(options) {
3538
process.stderr.setMaxListeners(50);
3639

3740
test('start', t => {
38-
const reporter = new MiniReporter();
41+
const reporter = new MiniReporter({color: true});
3942

4043
t.is(reporter.start(), ' \n ' + graySpinner + ' ');
4144
reporter.clearInterval();
@@ -432,7 +435,7 @@ test('results with errors and disabled code excerpts', t => {
432435
err2.expected = JSON.stringify([2]);
433436
err2.expectedType = 'array';
434437

435-
const reporter = miniReporter({basePath: path.dirname(err2Path)});
438+
const reporter = miniReporter({color: true, basePath: path.dirname(err2Path)});
436439
reporter.failCount = 1;
437440

438441
const runStatus = {
@@ -495,7 +498,7 @@ test('results with errors and broken code excerpts', t => {
495498
err2.expected = JSON.stringify([2]);
496499
err2.expectedType = 'array';
497500

498-
const reporter = miniReporter({basePath: path.dirname(err2Path)});
501+
const reporter = miniReporter({color: true, basePath: path.dirname(err2Path)});
499502
reporter.failCount = 1;
500503

501504
const runStatus = {
@@ -559,7 +562,7 @@ test('results with errors and disabled assert output', t => {
559562
err2.expected = JSON.stringify([2]);
560563
err2.expectedType = 'array';
561564

562-
const reporter = miniReporter({basePath: path.dirname(err1Path)});
565+
const reporter = miniReporter({color: true, basePath: path.dirname(err1Path)});
563566
reporter.failCount = 1;
564567

565568
const runStatus = {
@@ -731,7 +734,7 @@ test('results with watching enabled', t => {
731734
lolex.install(new Date(2014, 11, 19, 17, 19, 12, 200).getTime(), ['Date']);
732735
const time = ' ' + chalk.grey.dim('[17:19:12]');
733736

734-
const reporter = miniReporter({watching: true});
737+
const reporter = miniReporter({color: true, watching: true});
735738
reporter.passCount = 1;
736739
reporter.failCount = 0;
737740

@@ -905,3 +908,27 @@ test('results when hasExclusive is enabled, but there are multiple remaining tes
905908
t.is(actualOutput, expectedOutput);
906909
t.end();
907910
});
911+
912+
test('result when no-color flag is set', t => {
913+
const reporter = miniReporter({
914+
color: false
915+
});
916+
917+
const runStatus = {
918+
hasExclusive: true,
919+
testCount: 3,
920+
passCount: 1,
921+
failCount: 0,
922+
remainingCount: 2
923+
};
924+
925+
const output = reporter.finish(runStatus);
926+
const expectedOutput = [
927+
'',
928+
'',
929+
' The .only() modifier is used in some tests. 2 tests were not run',
930+
'\n'
931+
].join('\n');
932+
t.is(output, expectedOutput);
933+
t.end();
934+
});

test/reporters/verbose.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ lolex.install(new Date(2014, 11, 19, 17, 19, 12, 200).getTime(), ['Date']);
2323
const time = ' ' + chalk.grey.dim('[17:19:12]');
2424

2525
function createReporter(options) {
26+
if (options === undefined) {
27+
options = {color: true};
28+
}
2629
const reporter = new VerboseReporter(options);
2730
return reporter;
2831
}
@@ -378,7 +381,7 @@ test('results with errors', t => {
378381
error2.expected = JSON.stringify([2]);
379382
error2.expectedType = 'array';
380383

381-
const reporter = createReporter({basePath: path.dirname(err1Path)});
384+
const reporter = createReporter({color: true, basePath: path.dirname(err1Path)});
382385
const runStatus = createRunStatus();
383386
runStatus.failCount = 1;
384387
runStatus.tests = [{
@@ -439,7 +442,7 @@ test('results with errors and disabled code excerpts', t => {
439442
error2.expected = JSON.stringify([2]);
440443
error2.expectedType = 'array';
441444

442-
const reporter = createReporter({basePath: path.dirname(err2Path)});
445+
const reporter = createReporter({color: true, basePath: path.dirname(err2Path)});
443446
const runStatus = createRunStatus();
444447
runStatus.failCount = 1;
445448
runStatus.tests = [{
@@ -499,7 +502,7 @@ test('results with errors and disabled code excerpts', t => {
499502
error2.expected = JSON.stringify([2]);
500503
error2.expectedType = 'array';
501504

502-
const reporter = createReporter({basePath: path.dirname(err2Path)});
505+
const reporter = createReporter({color: true, basePath: path.dirname(err2Path)});
503506
const runStatus = createRunStatus();
504507
runStatus.failCount = 1;
505508
runStatus.tests = [{
@@ -560,7 +563,7 @@ test('results with errors and disabled assert output', t => {
560563
error2.expected = JSON.stringify([2]);
561564
error2.expectedType = 'array';
562565

563-
const reporter = createReporter({basePath: path.dirname(err1Path)});
566+
const reporter = createReporter({color: true, basePath: path.dirname(err1Path)});
564567
const runStatus = createRunStatus();
565568
runStatus.failCount = 1;
566569
runStatus.tests = [{
@@ -602,7 +605,7 @@ test('results with errors and disabled assert output', t => {
602605
});
603606

604607
test('results when fail-fast is enabled', t => {
605-
const reporter = new VerboseReporter();
608+
const reporter = createReporter();
606609
const runStatus = createRunStatus();
607610
runStatus.remainingCount = 1;
608611
runStatus.failCount = 1;
@@ -626,7 +629,7 @@ test('results when fail-fast is enabled', t => {
626629
});
627630

628631
test('results without fail-fast if no failing tests', t => {
629-
const reporter = new VerboseReporter();
632+
const reporter = createReporter();
630633
const runStatus = createRunStatus();
631634
runStatus.remainingCount = 1;
632635
runStatus.failCount = 0;
@@ -645,7 +648,7 @@ test('results without fail-fast if no failing tests', t => {
645648
});
646649

647650
test('results without fail-fast if no skipped tests', t => {
648-
const reporter = new VerboseReporter();
651+
const reporter = createReporter();
649652
const runStatus = createRunStatus();
650653
runStatus.remainingCount = 0;
651654
runStatus.failCount = 1;
@@ -715,15 +718,15 @@ test('full-width line when sectioning', t => {
715718

716719
test('write calls console.error', t => {
717720
const stub = sinon.stub(console, 'error');
718-
const reporter = new VerboseReporter();
721+
const reporter = createReporter();
719722
reporter.write('result');
720723
t.true(stub.called);
721724
console.error.restore();
722725
t.end();
723726
});
724727

725728
test('reporter.stdout and reporter.stderr both use process.stderr.write', t => {
726-
const reporter = new VerboseReporter();
729+
const reporter = createReporter();
727730
const stub = sinon.stub(process.stderr, 'write');
728731
reporter.stdout('result');
729732
reporter.stderr('result');
@@ -733,7 +736,7 @@ test('reporter.stdout and reporter.stderr both use process.stderr.write', t => {
733736
});
734737

735738
test('results when hasExclusive is enabled, but there are no known remaining tests', t => {
736-
const reporter = new VerboseReporter();
739+
const reporter = createReporter();
737740
const runStatus = createRunStatus();
738741
runStatus.hasExclusive = true;
739742
runStatus.passCount = 1;
@@ -750,7 +753,7 @@ test('results when hasExclusive is enabled, but there are no known remaining tes
750753
});
751754

752755
test('results when hasExclusive is enabled, but there is one remaining tests', t => {
753-
const reporter = new VerboseReporter();
756+
const reporter = createReporter();
754757
const runStatus = createRunStatus();
755758
runStatus.hasExclusive = true;
756759
runStatus.testCount = 2;
@@ -773,7 +776,7 @@ test('results when hasExclusive is enabled, but there is one remaining tests', t
773776
});
774777

775778
test('results when hasExclusive is enabled, but there are multiple remaining tests', t => {
776-
const reporter = new VerboseReporter();
779+
const reporter = createReporter();
777780
const runStatus = createRunStatus();
778781
runStatus.hasExclusive = true;
779782
runStatus.testCount = 3;
@@ -794,3 +797,26 @@ test('results when hasExclusive is enabled, but there are multiple remaining tes
794797
t.is(output, expectedOutput);
795798
t.end();
796799
});
800+
801+
test('result when no-color flag is set', t => {
802+
const reporter = new VerboseReporter({color: false});
803+
const runStatus = createRunStatus();
804+
runStatus.hasExclusive = true;
805+
runStatus.testCount = 3;
806+
runStatus.passCount = 1;
807+
runStatus.failCount = 0;
808+
runStatus.remainingCount = 2;
809+
810+
const output = reporter.finish(runStatus);
811+
const expectedOutput = [
812+
'',
813+
' 1 test passed [17:19:12]',
814+
'',
815+
'',
816+
' The .only() modifier is used in some tests. 2 tests were not run',
817+
''
818+
].join('\n');
819+
820+
t.is(output, expectedOutput);
821+
t.end();
822+
});

0 commit comments

Comments
 (0)