Skip to content

Commit 02b0aae

Browse files
novemberbornsindresorhus
authored andcommitted
Disable TAP reporter in watch mode (#732)
* disable tap reporter in watch mode Fixes #672 * write missing chokidar error to stderr
1 parent 3201b1b commit 02b0aae

File tree

9 files changed

+88
-10
lines changed

9 files changed

+88
-10
lines changed

cli.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var Promise = require('bluebird');
2525
var pkgConf = require('pkg-conf');
2626
var chalk = require('chalk');
2727
var isCi = require('is-ci');
28+
var hasFlag = require('has-flag');
2829
var colors = require('./lib/colors');
2930
var verboseReporter = require('./lib/reporters/verbose');
3031
var miniReporter = require('./lib/reporters/mini');
@@ -116,6 +117,14 @@ if (cli.flags.init) {
116117
return;
117118
}
118119

120+
if (
121+
(hasFlag('--watch') || hasFlag('-w')) && (hasFlag('--tap') || hasFlag('-t')) ||
122+
conf.watch && conf.tap
123+
) {
124+
console.error(' ' + colors.error(figures.cross) + ' The TAP reporter is not available when using watch mode.');
125+
process.exit(1);
126+
}
127+
119128
var api = new Api({
120129
failFast: cli.flags.failFast,
121130
serial: cli.flags.serial,
@@ -129,7 +138,7 @@ var api = new Api({
129138

130139
var reporter;
131140

132-
if (cli.flags.tap) {
141+
if (cli.flags.tap && !cli.flags.watch) {
133142
reporter = tapReporter();
134143
} else if (cli.flags.verbose || isCi) {
135144
reporter = verboseReporter();
@@ -167,7 +176,7 @@ if (cli.flags.watch) {
167176
} catch (err) {
168177
if (err.name === 'AvaError') {
169178
// An AvaError may be thrown if chokidar is not installed. Log it nicely.
170-
console.log(' ' + colors.error(figures.cross) + ' ' + err.message);
179+
console.error(' ' + colors.error(figures.cross) + ' ' + err.message);
171180
logger.exit(1);
172181
} else {
173182
// Rethrow so it becomes an uncaught exception.

docs/recipes/watch-mode.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ And then use:
4545
$ npm run test:watch
4646
```
4747

48+
Please note that the TAP reporter is unavailable when using watch mode.
49+
4850
## Requirements
4951

5052
AVA uses [`chokidar`] as the file watcher. It's configured as an optional dependency since `chokidar` sometimes can't be installed. Watch mode is not available if `chokidar` fails to install, instead you'll see a message like:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"find-cache-dir": "^0.1.1",
105105
"fn-name": "^2.0.0",
106106
"globby": "^4.0.0",
107+
"has-flag": "^2.0.0",
107108
"ignore-by-default": "^1.0.0",
108109
"is-ci": "^1.0.7",
109110
"is-generator-fn": "^1.0.0",

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ $ ava --tap | tap-nyan
676676

677677
<img src="media/tap-output.png" width="398">
678678

679+
Please note that the TAP reporter is unavailable when using [watch mode](#watch-it).
680+
679681
### Clean stack traces
680682

681683
AVA automatically removes unrelated lines in stack traces, allowing you to find the source of an error much faster.

test/cli.js

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ var cliPath = path.join(__dirname, '../cli.js');
1414
chalk.enabled = true;
1515
var colors = require('../lib/colors');
1616

17+
var hasChokidar = false;
18+
try {
19+
require('chokidar');
20+
hasChokidar = true;
21+
} catch (err) {}
22+
1723
function execCli(args, opts, cb) {
1824
var dirname;
1925
var env;
@@ -173,16 +179,10 @@ test('pkg-conf: cli takes precedence', function (t) {
173179
test('watcher works', function (t) {
174180
var killed = false;
175181

176-
var hasChokidar = false;
177-
try {
178-
require('chokidar');
179-
hasChokidar = true;
180-
} catch (err) {}
181-
182-
var child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher'}, function (err, stdout) {
182+
var child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher'}, function (err, stdout, stderr) {
183183
if (err && err.code === 1 && !hasChokidar) {
184184
t.comment('chokidar dependency is missing, cannot test watcher');
185-
t.match(stdout, 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.');
185+
t.match(stderr, 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.');
186186
t.end();
187187
} else {
188188
t.ok(killed);
@@ -208,6 +208,49 @@ test('watcher works', function (t) {
208208
});
209209
});
210210

211+
if (hasChokidar) {
212+
test('`"tap": true` config is ignored when --watch is given', function (t) {
213+
var killed = false;
214+
215+
var child = execCli(['--watch', 'test.js'], {dirname: 'fixture/watcher/tap-in-conf'}, function () {
216+
t.ok(killed);
217+
t.end();
218+
});
219+
220+
var combined = '';
221+
var testOutput = function (output) {
222+
combined += output;
223+
t.notMatch(combined, /TAP/);
224+
if (/works/.test(combined)) {
225+
child.kill();
226+
killed = true;
227+
}
228+
};
229+
child.stdout.on('data', testOutput);
230+
child.stderr.on('data', testOutput);
231+
});
232+
233+
test('bails when config contains `"tap": true` and `"watch": true`', function (t) {
234+
execCli(['test.js'], {dirname: 'fixture/watcher/tap-and-watch-in-conf'}, function (err, stdout, stderr) {
235+
t.is(err.code, 1);
236+
t.match(stderr, 'The TAP reporter is not available when using watch mode.');
237+
t.end();
238+
}).stderr.pipe(process.stderr);
239+
});
240+
241+
['--watch', '-w'].forEach(function (watchFlag) {
242+
['--tap', '-t'].forEach(function (tapFlag) {
243+
test('bails when ' + tapFlag + ' reporter is used while ' + watchFlag + ' is given', function (t) {
244+
execCli([tapFlag, watchFlag, 'test.js'], {dirname: 'fixture/watcher'}, function (err, stdout, stderr) {
245+
t.is(err.code, 1);
246+
t.match(stderr, 'The TAP reporter is not available when using watch mode.');
247+
t.end();
248+
}).stderr.pipe(process.stderr);
249+
});
250+
});
251+
});
252+
}
253+
211254
test('--match works', function (t) {
212255
execCli(['-m=foo', '-m=bar', '-m=!baz', '-m=t* a* f*', '-m=!t* a* n* f*', 'fixture/matcher-skip.js'], function (err) {
213256
t.ifError(err);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ava": {
3+
"tap": true,
4+
"watch": true
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from '../../../../';
2+
3+
test('works', t => {
4+
t.pass();
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ava": {
3+
"tap": true
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from '../../../../';
2+
3+
test('works', t => {
4+
t.pass();
5+
});

0 commit comments

Comments
 (0)