Skip to content

Commit 12c8d9e

Browse files
author
Thomas
committed
Added support for --no-color flag in verbose and mini reporters
1 parent 173da28 commit 12c8d9e

File tree

7 files changed

+88
-12
lines changed

7 files changed

+88
-12
lines changed

lib/cli.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ exports.run = () => {
3838
--verbose, -v Enable verbose output
3939
--no-cache Disable the transpiler cache
4040
--no-power-assert Disable Power Assert
41+
--no-color Disable color output
4142
--match, -m Only run tests with matching title (Can be repeated)
4243
--watch, -w Re-run tests when tests and source files change
4344
--source, -S Pattern to match source files so tests can be re-run (Can be repeated)
@@ -70,9 +71,10 @@ exports.run = () => {
7071
'tap',
7172
'verbose',
7273
'watch',
73-
'update-snapshots'
74+
'update-snapshots',
75+
'color'
7476
],
75-
default: conf,
77+
default: Object.assign({color: true}, conf),
7678
alias: {
7779
t: 'tap',
7880
v: 'verbose',
@@ -121,17 +123,23 @@ exports.run = () => {
121123
pkgDir,
122124
timeout: cli.flags.timeout,
123125
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0,
124-
updateSnapshots: cli.flags.updateSnapshots
126+
updateSnapshots: cli.flags.updateSnapshots,
127+
color: cli.flags.color
125128
});
126129

127130
let reporter;
128131

129132
if (cli.flags.tap && !cli.flags.watch) {
130133
reporter = tapReporter();
131134
} else if (cli.flags.verbose || isCi) {
132-
reporter = verboseReporter();
135+
reporter = verboseReporter({
136+
color: cli.flags.color
137+
});
133138
} else {
134-
reporter = miniReporter({watching: cli.flags.watch});
139+
reporter = miniReporter({
140+
watching: cli.flags.watch,
141+
color: cli.flags.color
142+
});
135143
}
136144

137145
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+
var 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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ var repeating = require('repeating');
1111
var colors = require('../colors');
1212

1313
chalk.enabled = true;
14-
Object.keys(colors).forEach(function (key) {
14+
for (let key of Object.keys(colors)) {
1515
colors[key].enabled = true;
16-
});
16+
}
1717

1818
function MiniReporter(options) {
1919
if (!(this instanceof MiniReporter)) {
@@ -28,6 +28,13 @@ function MiniReporter(options) {
2828

2929
this.options = Object.assign({}, options);
3030

31+
if (this.options.color === false) {
32+
chalk.enabled = false;
33+
for (let key of Object.keys(colors)) {
34+
colors[key].enabled = false;
35+
}
36+
}
37+
3138
this.reset();
3239
this.stream = process.stderr;
3340
this.stringDecoder = new StringDecoder();

lib/reporters/verbose.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ var plur = require('plur');
66
var repeating = require('repeating');
77
var colors = require('../colors');
88

9-
Object.keys(colors).forEach(function (key) {
9+
chalk.enabled = true;
10+
for (let key of Object.keys(colors)) {
1011
colors[key].enabled = true;
11-
});
12+
}
1213

13-
function VerboseReporter() {
14+
function VerboseReporter(options) {
1415
if (!(this instanceof VerboseReporter)) {
15-
return new VerboseReporter();
16+
return new VerboseReporter(options);
17+
}
18+
19+
this.options = Object.assign({}, options);
20+
21+
if (this.options.color === false) {
22+
chalk.enabled = false;
23+
for (let key of Object.keys(colors)) {
24+
colors[key].enabled = false;
25+
}
1626
}
1727
}
1828

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ $ ava --help
159159
--verbose, -v Enable verbose output
160160
--no-cache Disable the transpiler cache
161161
--no-power-assert Disable Power Assert
162+
--no-color Disable color output
162163
--match, -m Only run tests with matching title (Can be repeated)
163164
--watch, -w Re-run tests when tests and source files change
164165
--source, -S Pattern to match source files so tests can be re-run (Can be repeated)

test/reporters/mini.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,26 @@ test('results when hasExclusive is enabled, but there are multiple remaining tes
671671
t.end();
672672
});
673673

674+
test('Result when no-color flag is set', function (t) {
675+
var reporter = miniReporter({
676+
color: false
677+
});
678+
679+
var runStatus = {
680+
hasExclusive: true,
681+
testCount: 3,
682+
passCount: 1,
683+
failCount: 0,
684+
remainingCount: 2
685+
};
686+
687+
var output = reporter.finish(runStatus);
688+
var expectedOutput = [
689+
'',
690+
'',
691+
' The .only() modifier is used in some tests. 2 tests were not run',
692+
'\n'
693+
].join('\n');
694+
t.is(output, expectedOutput);
695+
t.end();
696+
});

test/reporters/verbose.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,31 @@ test('results when hasExclusive is enabled, but there are multiple remaining tes
571571
t.end();
572572
});
573573

574+
test('Result when no-color flag is set', function (t) {
575+
var reporter = verboseReporter({
576+
color: false
577+
});
578+
var runStatus = createRunStatus();
579+
runStatus.hasExclusive = true;
580+
runStatus.testCount = 3;
581+
runStatus.passCount = 1;
582+
runStatus.failCount = 0;
583+
runStatus.remainingCount = 2;
584+
585+
var output = reporter.finish(runStatus);
586+
var expectedOutput = [
587+
'',
588+
' 1 test passed [17:19:12]',
589+
'',
590+
'',
591+
' The .only() modifier is used in some tests. 2 tests were not run',
592+
''
593+
].join('\n');
594+
595+
t.is(output, expectedOutput);
596+
t.end();
597+
});
598+
574599
function fooFunc() {
575600
barFunc();
576601
}

0 commit comments

Comments
 (0)