Skip to content

Commit

Permalink
Add command for updating snapshots in watch mode (#1413)
Browse files Browse the repository at this point in the history
  • Loading branch information
efegurkan authored and novemberborn committed Jun 25, 2017
1 parent 87eef84 commit f507e36
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 41 deletions.
7 changes: 6 additions & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class Api extends EventEmitter {
precompiled[resolvedfpath] = hash;

const options = Object.assign({}, this.options, {precompiled});
if (runStatus.updateSnapshots) {
// Don't use in Object.assign() since it'll override options.updateSnapshots even when false.
options.updateSnapshots = true;
}
const emitter = fork(file, options, execArgv);
runStatus.observeFork(emitter);

Expand Down Expand Up @@ -137,7 +141,8 @@ class Api extends EventEmitter {
runOnlyExclusive: options.runOnlyExclusive,
prefixTitles: this.options.explicitTitles || files.length > 1,
base: path.relative(process.cwd(), commonPathPrefix(files)) + path.sep,
failFast: this.options.failFast
failFast: this.options.failFast,
updateSnapshots: options.updateSnapshots
});

this.emit('test-run', runStatus, files);
Expand Down
4 changes: 4 additions & 0 deletions docs/recipes/watch-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ If you run AVA in your CI with watch mode, the execution will exit with an error

You can quickly rerun all tests by typing <kbd>r</kbd> on the console, followed by <kbd>Enter</kbd>.

## Updating snapshots

You can update failing snapshots by typing <kbd>u</kbd> on the console, followed by <kbd>Enter</kbd>.

## Debugging

Sometimes watch mode does something surprising like rerunning all tests when you thought only a single test would be run. To see its reasoning you can enable a debug mode. This will work best with the verbose reporter:
Expand Down
1 change: 1 addition & 0 deletions lib/run-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class RunStatus extends EventEmitter {
this.stats = [];
this.tests = [];
this.failFastEnabled = opts.failFast || false;
this.updateSnapshots = opts.updateSnapshots || false;

autoBind(this);
}
Expand Down
18 changes: 14 additions & 4 deletions lib/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class Watcher {

this.clearLogOnNextRun = true;
this.runVector = 0;
this.run = specificFiles => {
this.previousFiles = files;
this.run = (specificFiles, updateSnapshots) => {
if (this.runVector > 0) {
const cleared = this.clearLogOnNextRun && logger.clear();
if (!cleared) {
Expand Down Expand Up @@ -117,7 +118,8 @@ class Watcher {
}

this.touchedFiles.clear();
this.busy = api.run(specificFiles || files, {runOnlyExclusive})
this.previousFiles = specificFiles || files;
this.busy = api.run(this.previousFiles, {runOnlyExclusive, updateSnapshots: updateSnapshots === true})
.then(runStatus => {
runStatus.previousFailCount = this.sumPreviousFailures(currentVector);
logger.finish(runStatus);
Expand Down Expand Up @@ -273,7 +275,7 @@ class Watcher {

stdin.on('data', data => {
data = data.trim().toLowerCase();
if (data !== 'r' && data !== 'rs') {
if (data !== 'r' && data !== 'rs' && data !== 'u') {
return;
}

Expand All @@ -285,14 +287,22 @@ class Watcher {
// the busy promise to fulfil
this.debouncer.cancel();
this.clearLogOnNextRun = false;
this.rerunAll();
if (data === 'u') {
this.updatePreviousSnapshots();
} else {
this.rerunAll();
}
});
});
}
rerunAll() {
this.dirtyStates = {};
this.run();
}
updatePreviousSnapshots() {
this.dirtyStates = {};
this.run(this.previousFiles, true);
}
runAfterChanges() {
const dirtyStates = this.dirtyStates;
this.dirtyStates = {};
Expand Down
Loading

0 comments on commit f507e36

Please sign in to comment.