Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command for updating snapshots in watch mode #1413

Merged
merged 4 commits into from
Jun 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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