Skip to content

Commit f507e36

Browse files
efegurkannovemberborn
authored andcommitted
Add command for updating snapshots in watch mode (#1413)
1 parent 87eef84 commit f507e36

File tree

5 files changed

+91
-41
lines changed

5 files changed

+91
-41
lines changed

api.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class Api extends EventEmitter {
6060
precompiled[resolvedfpath] = hash;
6161

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

@@ -137,7 +141,8 @@ class Api extends EventEmitter {
137141
runOnlyExclusive: options.runOnlyExclusive,
138142
prefixTitles: this.options.explicitTitles || files.length > 1,
139143
base: path.relative(process.cwd(), commonPathPrefix(files)) + path.sep,
140-
failFast: this.options.failFast
144+
failFast: this.options.failFast,
145+
updateSnapshots: options.updateSnapshots
141146
});
142147

143148
this.emit('test-run', runStatus, files);

docs/recipes/watch-mode.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ If you run AVA in your CI with watch mode, the execution will exit with an error
9191

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

94+
## Updating snapshots
95+
96+
You can update failing snapshots by typing <kbd>u</kbd> on the console, followed by <kbd>Enter</kbd>.
97+
9498
## Debugging
9599

96100
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:

lib/run-status.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class RunStatus extends EventEmitter {
4040
this.stats = [];
4141
this.tests = [];
4242
this.failFastEnabled = opts.failFast || false;
43+
this.updateSnapshots = opts.updateSnapshots || false;
4344

4445
autoBind(this);
4546
}

lib/watcher.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class Watcher {
8484

8585
this.clearLogOnNextRun = true;
8686
this.runVector = 0;
87-
this.run = specificFiles => {
87+
this.previousFiles = files;
88+
this.run = (specificFiles, updateSnapshots) => {
8889
if (this.runVector > 0) {
8990
const cleared = this.clearLogOnNextRun && logger.clear();
9091
if (!cleared) {
@@ -117,7 +118,8 @@ class Watcher {
117118
}
118119

119120
this.touchedFiles.clear();
120-
this.busy = api.run(specificFiles || files, {runOnlyExclusive})
121+
this.previousFiles = specificFiles || files;
122+
this.busy = api.run(this.previousFiles, {runOnlyExclusive, updateSnapshots: updateSnapshots === true})
121123
.then(runStatus => {
122124
runStatus.previousFailCount = this.sumPreviousFailures(currentVector);
123125
logger.finish(runStatus);
@@ -273,7 +275,7 @@ class Watcher {
273275

274276
stdin.on('data', data => {
275277
data = data.trim().toLowerCase();
276-
if (data !== 'r' && data !== 'rs') {
278+
if (data !== 'r' && data !== 'rs' && data !== 'u') {
277279
return;
278280
}
279281

@@ -285,14 +287,22 @@ class Watcher {
285287
// the busy promise to fulfil
286288
this.debouncer.cancel();
287289
this.clearLogOnNextRun = false;
288-
this.rerunAll();
290+
if (data === 'u') {
291+
this.updatePreviousSnapshots();
292+
} else {
293+
this.rerunAll();
294+
}
289295
});
290296
});
291297
}
292298
rerunAll() {
293299
this.dirtyStates = {};
294300
this.run();
295301
}
302+
updatePreviousSnapshots() {
303+
this.dirtyStates = {};
304+
this.run(this.previousFiles, true);
305+
}
296306
runAfterChanges() {
297307
const dirtyStates = this.dirtyStates;
298308
this.dirtyStates = {};

0 commit comments

Comments
 (0)