diff --git a/lib/watcher.js b/lib/watcher.js index ab42f01..aa17d9a 100644 --- a/lib/watcher.js +++ b/lib/watcher.js @@ -125,9 +125,11 @@ Watcher.prototype.trackTestDependencies = function (api, sources) { return nodePath.relative(cwd, absPath); }; - api.on('dependencies', function (file, dependencies) { - var sourceDeps = dependencies.map(relative).filter(isSource); - self.updateTestDependencies(file, sourceDeps); + api.on('test-run', function (runStatus) { + runStatus.on('dependencies', function (file, dependencies) { + var sourceDeps = dependencies.map(relative).filter(isSource); + self.updateTestDependencies(file, sourceDeps); + }); }); }; diff --git a/test/cli.js b/test/cli.js index c8e4146..ee82e62 100644 --- a/test/cli.js +++ b/test/cli.js @@ -176,7 +176,7 @@ test('pkg-conf: cli takes precedence', function (t) { }); }); -test('watcher works', function (t) { +test("watcher reruns test files when they changed", function (t) { var killed = false; var child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher'}, function (err, stdout, stderr) { @@ -209,6 +209,30 @@ test('watcher works', function (t) { }); if (hasChokidar) { + test("watcher reruns test files when source dependencies change", function (t) { + var killed = false; + + var child = execCli(['--verbose', '--watch', '--source=source.js', 'test-*.js'], {dirname: 'fixture/watcher/with-dependencies'}, function (err) { + t.ok(killed); + t.ifError(err); + t.end(); + }); + + var buffer = ''; + var passedFirst = false; + child.stderr.on('data', function (str) { + buffer += str; + if (/2 tests passed/.test(buffer) && !passedFirst) { + touch.sync(path.join(__dirname, 'fixture/watcher/with-dependencies/source.js')); + buffer = ''; + passedFirst = true; + } else if (/1 test passed/.test(buffer) && !killed) { + child.kill(); + killed = true; + } + }); + }); + test('`"tap": true` config is ignored when --watch is given', function (t) { var killed = false; diff --git a/test/fixture/watcher/with-dependencies/source.js b/test/fixture/watcher/with-dependencies/source.js new file mode 100644 index 0000000..80b9ab8 --- /dev/null +++ b/test/fixture/watcher/with-dependencies/source.js @@ -0,0 +1,2 @@ +'use strict'; +module.exports = true; diff --git a/test/fixture/watcher/with-dependencies/test-1.js b/test/fixture/watcher/with-dependencies/test-1.js new file mode 100644 index 0000000..222d02f --- /dev/null +++ b/test/fixture/watcher/with-dependencies/test-1.js @@ -0,0 +1,6 @@ +import test from '../../../../'; +import dependency from './source.js'; + +test('works', t => { + t.truthy(dependency); +}); diff --git a/test/fixture/watcher/with-dependencies/test-2.js b/test/fixture/watcher/with-dependencies/test-2.js new file mode 100644 index 0000000..c5af73c --- /dev/null +++ b/test/fixture/watcher/with-dependencies/test-2.js @@ -0,0 +1,5 @@ +import test from '../../../../'; + +test('works', t => { + t.pass(); +}); diff --git a/test/watcher.js b/test/watcher.js index 3f1d7d5..c93b6aa 100644 --- a/test/watcher.js +++ b/test/watcher.js @@ -812,15 +812,23 @@ group('chokidar is installed', function (beforeEach, test, group) { group('tracks test dependencies', function (beforeEach, test) { var apiEmitter; + var runStatus; + var runStatusEmitter; beforeEach(function () { apiEmitter = new EventEmitter(); api.on = function (event, fn) { apiEmitter.on(event, fn); }; + runStatusEmitter = new EventEmitter(); + runStatus = { + on: function (event, fn) { + runStatusEmitter.on(event, fn); + } + }; }); var emitDependencies = function (file, dependencies) { - apiEmitter.emit('dependencies', file, dependencies); + runStatusEmitter.emit('dependencies', file, dependencies); }; var seed = function (sources) { @@ -832,8 +840,13 @@ group('chokidar is installed', function (beforeEach, test, group) { })); var watcher = start(sources); - emitDependencies(path.join('test', '1.js'), [path.resolve('dep-1.js'), path.resolve('dep-3.js')]); - emitDependencies(path.join('test', '2.js'), [path.resolve('dep-2.js'), path.resolve('dep-3.js')]); + var files = [path.join('test', '1.js'), path.join('test', '2.js')]; + var absFiles = files.map(function (relFile) { + return path.resolve(relFile); + }); + apiEmitter.emit('test-run', runStatus, absFiles); + emitDependencies(files[0], [path.resolve('dep-1.js'), path.resolve('dep-3.js')]); + emitDependencies(files[1], [path.resolve('dep-2.js'), path.resolve('dep-3.js')]); done(); api.run.returns(new Promise(function () {}));