Skip to content

Commit

Permalink
Treat loaded snapshot files as test dependencies
Browse files Browse the repository at this point in the history
This enables the watcher to rerun the correct test when a snapshot file
is modified.
  • Loading branch information
novemberborn committed Jun 25, 2017
1 parent 50b60a1 commit dbc78dc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/process-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ exports.installDependencyTracking = (dependencies, testPath) => {

require.extensions[ext] = (module, filename) => {
if (filename !== testPath) {
dependencies.push(filename);
dependencies.add(filename);
}

wrappedHandler(module, filename);
Expand Down
1 change: 1 addition & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class Runner extends EventEmitter {
const dir = path.join(path.dirname(this.file), '__snapshots__');
const relFile = path.relative(this.projectDir, this.file);
this.snapshots = snapshotManager.load(dir, name, relFile, this.updateSnapshots);
this.emit('dependency', this.snapshots.snapPath);
}

return this.snapshots.compare(options);
Expand Down
6 changes: 4 additions & 2 deletions lib/snapshot-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ class Manager {
this.relFile = options.relFile;
this.reportFile = options.reportFile;
this.snapFile = options.snapFile;
this.snapPath = options.snapPath;
this.snapshotsByHash = options.snapshotsByHash;

this.hasChanges = false;
Expand Down Expand Up @@ -336,7 +337,7 @@ class Manager {
return null;
}

const snapPath = path.join(this.dir, this.snapFile);
const snapPath = this.snapPath;
const buffer = encodeSnapshots(this.snapshotsByHash);

const reportPath = path.join(this.dir, this.reportFile);
Expand All @@ -356,12 +357,12 @@ class Manager {
function load(dir, name, relFile, updating) {
const reportFile = `${name}.md`;
const snapFile = `${name}.snap`;
const snapPath = path.join(dir, snapFile);

let appendOnly = !updating;
let snapshotsByHash;

if (!updating) {
const snapPath = path.join(dir, snapFile);
const buffer = tryRead(snapPath);
if (buffer) {
snapshotsByHash = decodeSnapshots(buffer, snapPath);
Expand All @@ -376,6 +377,7 @@ function load(dir, name, relFile, updating) {
relFile,
reportFile,
snapFile,
snapPath,
snapshotsByHash: snapshotsByHash || new Map()
});
}
Expand Down
10 changes: 8 additions & 2 deletions lib/test-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ adapter.installPrecompilerHook();

const testPath = opts.file;

const dependencies = [];
const dependencies = new Set();
adapter.installDependencyTracking(dependencies, testPath);

const touchedFiles = new Set();
Expand All @@ -48,6 +48,9 @@ const touchedFiles = new Set();
let runner = null;
exports.setRunner = newRunner => {
runner = newRunner;
runner.on('dependency', file => {
dependencies.add(file);
});
runner.on('touched', files => {
for (const file of files) {
touchedFiles.add(file);
Expand Down Expand Up @@ -132,7 +135,10 @@ process.on('ava-teardown', () => {
// set of dependencies is included no matter how the process exits, unless
// it flat out crashes. Also include any files that AVA touched during the
// test run. This allows the watcher to ignore modifications to those files.
adapter.send('teardown', {dependencies, touchedFiles: Array.from(touchedFiles)});
adapter.send('teardown', {
dependencies: Array.from(dependencies),
touchedFiles: Array.from(touchedFiles)
});
});

process.on('ava-exit', () => {
Expand Down
28 changes: 28 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,34 @@ test('watcher does not rerun test files when they write snapshot files', t => {
});
});

test('watcher reruns test files when snapshot dependencies change', t => {
let killed = false;

const child = execCli(['--verbose', '--watch', '--update-snapshots', 'test.js'], {dirname: 'fixture/snapshots'}, err => {
t.ok(killed);
t.ifError(err);
t.end();
});

let buffer = '';
let passedFirst = false;
child.stderr.on('data', str => {
buffer += str;
if (/2 tests passed/.test(buffer)) {
buffer = '';
if (passedFirst) {
child.kill();
killed = true;
} else {
passedFirst = true;
setTimeout(() => {
touch.sync(path.join(__dirname, 'fixture/snapshots/__snapshots__/test.js.snap'));
}, 500);
}
}
});
});

test('`"tap": true` config is ignored when --watch is given', t => {
let killed = false;

Expand Down

0 comments on commit dbc78dc

Please sign in to comment.