Skip to content

Commit

Permalink
Merge pull request #541 from novemberborn/cli-watcher-test
Browse files Browse the repository at this point in the history
Test watcher from the CLI
  • Loading branch information
sindresorhus committed Feb 11, 2016
2 parents 9c22ee4 + b8c444d commit 93af8d8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -137,6 +137,7 @@
"sinon": "^1.17.2",
"source-map-fixtures": "^1.0.0",
"tap": "^5.4.2",
"touch": "^1.0.0",
"xo": "*",
"zen-observable": "^0.1.6"
},
Expand Down
47 changes: 46 additions & 1 deletion test/cli.js
Expand Up @@ -5,6 +5,7 @@ var test = require('tap').test;
global.Promise = require('bluebird');
var getStream = require('get-stream');
var arrify = require('arrify');
var touch = require('touch');
var cliPath = path.join(__dirname, '../cli.js');

function execCli(args, dirname, cb) {
Expand All @@ -21,11 +22,12 @@ function execCli(args, dirname, cb) {
env.AVA_APPVEYOR = 1;
}

var child;
var stdout;
var stderr;

var processPromise = new Promise(function (resolve) {
var child = childProcess.spawn(process.execPath, [path.relative(dirname, cliPath)].concat(arrify(args)), {
child = childProcess.spawn(process.execPath, [path.relative(dirname, cliPath)].concat(arrify(args)), {
cwd: dirname,
env: env,
stdio: [null, 'pipe', 'pipe']
Expand All @@ -49,6 +51,8 @@ function execCli(args, dirname, cb) {
Promise.all([processPromise, stdout, stderr]).then(function (args) {
cb.apply(null, args);
});

return child;
}

test('throwing a named function will report the to the console', function (t) {
Expand Down Expand Up @@ -110,3 +114,44 @@ test('pkg-conf: cli takes precedence', function (t) {
t.end();
});
});

test('watcher works', function (t) {
var killed = false;

var hasChokidar = false;
try {
require('chokidar');
hasChokidar = true;
} catch (err) {}

var child = execCli(['--verbose', '--watch', 'test.js'], 'fixture/watcher', function (err, stdout) {
if (err && err.code === 1 && !hasChokidar) {
t.comment('chokidar dependency is missing, cannot test watcher');
t.match(stdout, 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.');
t.end();
} else {
t.ok(killed);
t.ifError(err);
t.end();
}
});

var buffer = '';
var passedFirst = false;
// Pause the stream before attaching the 'data' listener. execCli() uses
// get-stream which read()s from the stream. The test just needs to piggyback
// on that without switching the stream to flowing mode.
child.stderr.pause().on('data', function (str) {
buffer += str;
if (/1 test passed/.test(str)) {
if (!passedFirst) {
touch.sync(path.join(__dirname, 'fixture/watcher/test.js'));
buffer = '';
passedFirst = true;
} else if (!killed) {
child.kill();
killed = true;
}
}
});
});
5 changes: 5 additions & 0 deletions test/fixture/watcher/test.js
@@ -0,0 +1,5 @@
import test from '../../../';

test('works', t => {
t.pass();
});

0 comments on commit 93af8d8

Please sign in to comment.