Skip to content

Commit 93af8d8

Browse files
committed
Merge pull request #541 from novemberborn/cli-watcher-test
Test watcher from the CLI
2 parents 9c22ee4 + b8c444d commit 93af8d8

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
"sinon": "^1.17.2",
138138
"source-map-fixtures": "^1.0.0",
139139
"tap": "^5.4.2",
140+
"touch": "^1.0.0",
140141
"xo": "*",
141142
"zen-observable": "^0.1.6"
142143
},

test/cli.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var test = require('tap').test;
55
global.Promise = require('bluebird');
66
var getStream = require('get-stream');
77
var arrify = require('arrify');
8+
var touch = require('touch');
89
var cliPath = path.join(__dirname, '../cli.js');
910

1011
function execCli(args, dirname, cb) {
@@ -21,11 +22,12 @@ function execCli(args, dirname, cb) {
2122
env.AVA_APPVEYOR = 1;
2223
}
2324

25+
var child;
2426
var stdout;
2527
var stderr;
2628

2729
var processPromise = new Promise(function (resolve) {
28-
var child = childProcess.spawn(process.execPath, [path.relative(dirname, cliPath)].concat(arrify(args)), {
30+
child = childProcess.spawn(process.execPath, [path.relative(dirname, cliPath)].concat(arrify(args)), {
2931
cwd: dirname,
3032
env: env,
3133
stdio: [null, 'pipe', 'pipe']
@@ -49,6 +51,8 @@ function execCli(args, dirname, cb) {
4951
Promise.all([processPromise, stdout, stderr]).then(function (args) {
5052
cb.apply(null, args);
5153
});
54+
55+
return child;
5256
}
5357

5458
test('throwing a named function will report the to the console', function (t) {
@@ -110,3 +114,44 @@ test('pkg-conf: cli takes precedence', function (t) {
110114
t.end();
111115
});
112116
});
117+
118+
test('watcher works', function (t) {
119+
var killed = false;
120+
121+
var hasChokidar = false;
122+
try {
123+
require('chokidar');
124+
hasChokidar = true;
125+
} catch (err) {}
126+
127+
var child = execCli(['--verbose', '--watch', 'test.js'], 'fixture/watcher', function (err, stdout) {
128+
if (err && err.code === 1 && !hasChokidar) {
129+
t.comment('chokidar dependency is missing, cannot test watcher');
130+
t.match(stdout, 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.');
131+
t.end();
132+
} else {
133+
t.ok(killed);
134+
t.ifError(err);
135+
t.end();
136+
}
137+
});
138+
139+
var buffer = '';
140+
var passedFirst = false;
141+
// Pause the stream before attaching the 'data' listener. execCli() uses
142+
// get-stream which read()s from the stream. The test just needs to piggyback
143+
// on that without switching the stream to flowing mode.
144+
child.stderr.pause().on('data', function (str) {
145+
buffer += str;
146+
if (/1 test passed/.test(str)) {
147+
if (!passedFirst) {
148+
touch.sync(path.join(__dirname, 'fixture/watcher/test.js'));
149+
buffer = '';
150+
passedFirst = true;
151+
} else if (!killed) {
152+
child.kill();
153+
killed = true;
154+
}
155+
}
156+
});
157+
});

test/fixture/watcher/test.js

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)