Skip to content

Commit fa85489

Browse files
forresstnovemberborn
authored andcommitted
Chokidar shouldn't be an optional dependency (avajs#916)
1 parent 6669a34 commit fa85489

File tree

5 files changed

+62
-107
lines changed

5 files changed

+62
-107
lines changed

docs/recipes/watch-mode.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ Please note that the TAP reporter is unavailable when using watch mode.
5959

6060
## Requirements
6161

62-
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:
63-
64-
> The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.
65-
66-
Please refer to the [`chokidar` documentation][`chokidar`] for how to resolve this problem.
62+
AVA uses [`chokidar`] as the file watcher. Note that even if you see warnings about optional dependencies failing during install, it will still work fine. Please refer to the *[Install Troubleshooting]* section of `chokidar` documentation for how to resolve the installation problems with chokidar.
6763

6864
## Source files and test files
6965

@@ -111,6 +107,7 @@ $ npm test -- --watch --verbose
111107
Watch mode is relatively new and there might be some rough edges. Please [report](https://github.com/avajs/ava/issues) any issues you encounter. Thanks!
112108

113109
[`chokidar`]: https://github.com/paulmillr/chokidar
110+
[Install Troubleshooting]: https://github.com/paulmillr/chokidar#install-troubleshooting
114111
[`ignore-by-default`]: https://github.com/novemberborn/ignore-by-default
115112
[`--require` CLI flag]: https://github.com/avajs/ava#cli
116113
[`--source` CLI flag]: https://github.com/avajs/ava#cli

lib/watcher.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22
var nodePath = require('path');
33
var debug = require('debug')('ava:watcher');
44
var diff = require('lodash.difference');
5+
var chokidar = require('chokidar');
56
var flatten = require('arr-flatten');
67
var union = require('array-union');
78
var uniq = require('array-uniq');
8-
var AvaError = require('./ava-error');
99
var AvaFiles = require('./ava-files');
1010

11-
function requireChokidar() {
12-
try {
13-
return require('chokidar');
14-
} catch (err) {
15-
throw new AvaError('The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.');
16-
}
17-
}
18-
1911
function rethrowAsync(err) {
2012
// Don't swallow exceptions. Note that any expected error should already have
2113
// been logged.
@@ -96,7 +88,7 @@ Watcher.prototype.watchFiles = function () {
9688
var self = this;
9789
var patterns = this.avaFiles.getChokidarPatterns();
9890

99-
requireChokidar().watch(patterns.paths, {
91+
chokidar.watch(patterns.paths, {
10092
ignored: patterns.ignored,
10193
ignoreInitial: true
10294
}).on('all', function (event, path) {

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"bluebird": "^3.0.0",
9595
"caching-transform": "^1.0.0",
9696
"chalk": "^1.0.0",
97+
"chokidar": "^1.4.2",
9798
"clean-yaml-object": "^0.1.0",
9899
"cli-cursor": "^1.0.2",
99100
"cli-spinners": "^0.1.2",
@@ -174,9 +175,6 @@
174175
"xo": "*",
175176
"zen-observable": "^0.2.1"
176177
},
177-
"optionalDependencies": {
178-
"chokidar": "^1.4.2"
179-
},
180178
"xo": {
181179
"rules": {
182180
"import/newline-after-import": 0

test/cli.js

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

18-
var hasChokidar = false;
19-
try {
20-
require('chokidar');
21-
hasChokidar = true;
22-
} catch (err) {}
23-
2418
function execCli(args, opts, cb) {
2519
var dirname;
2620
var env;
@@ -180,16 +174,10 @@ test('pkg-conf: cli takes precedence', function (t) {
180174
test('watcher reruns test files when they changed', function (t) {
181175
var killed = false;
182176

183-
var child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher'}, function (err, stdout, stderr) {
184-
if (err && err.code === 1 && !hasChokidar) {
185-
t.comment('chokidar dependency is missing, cannot test watcher');
186-
t.match(stderr, 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.');
187-
t.end();
188-
} else {
189-
t.ok(killed);
190-
t.ifError(err);
191-
t.end();
192-
}
177+
var child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher'}, function (err) {
178+
t.ok(killed);
179+
t.ifError(err);
180+
t.end();
193181
});
194182

195183
var buffer = '';
@@ -209,72 +197,70 @@ test('watcher reruns test files when they changed', function (t) {
209197
});
210198
});
211199

212-
if (hasChokidar) {
213-
test('watcher reruns test files when source dependencies change', function (t) {
214-
var killed = false;
215-
216-
var child = execCli(['--verbose', '--watch', '--source=source.js', 'test-*.js'], {dirname: 'fixture/watcher/with-dependencies'}, function (err) {
217-
t.ok(killed);
218-
t.ifError(err);
219-
t.end();
220-
});
200+
test('watcher reruns test files when source dependencies change', function (t) {
201+
var killed = false;
221202

222-
var buffer = '';
223-
var passedFirst = false;
224-
child.stderr.on('data', function (str) {
225-
buffer += str;
226-
if (/2 tests passed/.test(buffer) && !passedFirst) {
227-
touch.sync(path.join(__dirname, 'fixture/watcher/with-dependencies/source.js'));
228-
buffer = '';
229-
passedFirst = true;
230-
} else if (/1 test passed/.test(buffer) && !killed) {
231-
child.kill();
232-
killed = true;
233-
}
234-
});
203+
var child = execCli(['--verbose', '--watch', '--source=source.js', 'test-*.js'], {dirname: 'fixture/watcher/with-dependencies'}, function (err) {
204+
t.ok(killed);
205+
t.ifError(err);
206+
t.end();
235207
});
236208

237-
test('`"tap": true` config is ignored when --watch is given', function (t) {
238-
var killed = false;
209+
var buffer = '';
210+
var passedFirst = false;
211+
child.stderr.on('data', function (str) {
212+
buffer += str;
213+
if (/2 tests passed/.test(buffer) && !passedFirst) {
214+
touch.sync(path.join(__dirname, 'fixture/watcher/with-dependencies/source.js'));
215+
buffer = '';
216+
passedFirst = true;
217+
} else if (/1 test passed/.test(buffer) && !killed) {
218+
child.kill();
219+
killed = true;
220+
}
221+
});
222+
});
239223

240-
var child = execCli(['--watch', 'test.js'], {dirname: 'fixture/watcher/tap-in-conf'}, function () {
241-
t.ok(killed);
242-
t.end();
243-
});
224+
test('`"tap": true` config is ignored when --watch is given', function (t) {
225+
var killed = false;
244226

245-
var combined = '';
246-
var testOutput = function (output) {
247-
combined += output;
248-
t.notMatch(combined, /TAP/);
249-
if (/works/.test(combined)) {
250-
child.kill();
251-
killed = true;
252-
}
253-
};
254-
child.stdout.on('data', testOutput);
255-
child.stderr.on('data', testOutput);
227+
var child = execCli(['--watch', 'test.js'], {dirname: 'fixture/watcher/tap-in-conf'}, function () {
228+
t.ok(killed);
229+
t.end();
256230
});
257231

258-
test('bails when config contains `"tap": true` and `"watch": true`', function (t) {
259-
execCli(['test.js'], {dirname: 'fixture/watcher/tap-and-watch-in-conf'}, function (err, stdout, stderr) {
260-
t.is(err.code, 1);
261-
t.match(stderr, 'The TAP reporter is not available when using watch mode.');
262-
t.end();
263-
});
232+
var combined = '';
233+
var testOutput = function (output) {
234+
combined += output;
235+
t.notMatch(combined, /TAP/);
236+
if (/works/.test(combined)) {
237+
child.kill();
238+
killed = true;
239+
}
240+
};
241+
child.stdout.on('data', testOutput);
242+
child.stderr.on('data', testOutput);
243+
});
244+
245+
test('bails when config contains `"tap": true` and `"watch": true`', function (t) {
246+
execCli(['test.js'], {dirname: 'fixture/watcher/tap-and-watch-in-conf'}, function (err, stdout, stderr) {
247+
t.is(err.code, 1);
248+
t.match(stderr, 'The TAP reporter is not available when using watch mode.');
249+
t.end();
264250
});
251+
});
265252

266-
['--watch', '-w'].forEach(function (watchFlag) {
267-
['--tap', '-t'].forEach(function (tapFlag) {
268-
test('bails when ' + tapFlag + ' reporter is used while ' + watchFlag + ' is given', function (t) {
269-
execCli([tapFlag, watchFlag, 'test.js'], {dirname: 'fixture/watcher'}, function (err, stdout, stderr) {
270-
t.is(err.code, 1);
271-
t.match(stderr, 'The TAP reporter is not available when using watch mode.');
272-
t.end();
273-
});
253+
['--watch', '-w'].forEach(function (watchFlag) {
254+
['--tap', '-t'].forEach(function (tapFlag) {
255+
test('bails when ' + tapFlag + ' reporter is used while ' + watchFlag + ' is given', function (t) {
256+
execCli([tapFlag, watchFlag, 'test.js'], {dirname: 'fixture/watcher'}, function (err, stdout, stderr) {
257+
t.is(err.code, 1);
258+
t.match(stderr, 'The TAP reporter is not available when using watch mode.');
259+
t.end();
274260
});
275261
});
276262
});
277-
}
263+
});
278264

279265
test('--match works', function (t) {
280266
execCli(['-m=foo', '-m=bar', '-m=!baz', '-m=t* a* f*', '-m=!t* a* n* f*', 'fixture/matcher-skip.js'], function (err) {

test/watcher.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,7 @@ function makeGroup(test) {
3636
}
3737
var group = makeGroup(test);
3838

39-
test('chokidar is not installed', function (t) {
40-
t.plan(2);
41-
42-
var Subject = proxyquire.noCallThru().load('../lib/watcher', {
43-
chokidar: null
44-
});
45-
46-
try {
47-
new Subject({}, { // eslint-disable-line no-new
48-
excludePatterns: [],
49-
on: function () {}
50-
}, [], []);
51-
} catch (err) {
52-
t.is(err.name, 'AvaError');
53-
t.is(err.message, 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.');
54-
}
55-
});
56-
57-
group('chokidar is installed', function (beforeEach, test, group) {
39+
group('chokidar', function (beforeEach, test, group) {
5840
var chokidar;
5941
var debug;
6042
var logger;

0 commit comments

Comments
 (0)