Browse Source

ensure watcher receives dependencies again (#756)

Since #713 the API no longer emits 'dependencies' events. These are emitted
instead by the RunStatus, which can be obtained by listening to the 'test-run'
event on the API.

The watcher tests use a mocked API object which wasn't updated to reflect these
changes. Consequently dependency tracking was broken since #713 was merged.

This commit fixes the watcher and the corresponding tests. I've also added an
integration test which does not rely on mocking, helping us detect breakage
sooner.
browser-support
Mark Wubben 9 years ago
committed by James Talmage
parent
commit
f5246e173c
  1. 4
      lib/watcher.js
  2. 26
      test/cli.js
  3. 2
      test/fixture/watcher/with-dependencies/source.js
  4. 6
      test/fixture/watcher/with-dependencies/test-1.js
  5. 5
      test/fixture/watcher/with-dependencies/test-2.js
  6. 19
      test/watcher.js

4
lib/watcher.js

@ -125,10 +125,12 @@ Watcher.prototype.trackTestDependencies = function (api, sources) {
return nodePath.relative(cwd, absPath);
};
api.on('dependencies', function (file, dependencies) {
api.on('test-run', function (runStatus) {
runStatus.on('dependencies', function (file, dependencies) {
var sourceDeps = dependencies.map(relative).filter(isSource);
self.updateTestDependencies(file, sourceDeps);
});
});
};
Watcher.prototype.updateTestDependencies = function (file, sources) {

26
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;

2
test/fixture/watcher/with-dependencies/source.js

@ -0,0 +1,2 @@
'use strict';
module.exports = true;

6
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);
});

5
test/fixture/watcher/with-dependencies/test-2.js

@ -0,0 +1,5 @@
import test from '../../../../';
test('works', t => {
t.pass();
});

19
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 () {}));

Loading…
Cancel
Save