/
update_snapshots_interactive.ts
103 lines (91 loc) · 2.92 KB
/
update_snapshots_interactive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {Config} from '@jest/types';
import {AggregatedResult, AssertionLocation} from '@jest/test-result';
import {BaseWatchPlugin, JestHookSubscriber} from 'jest-watcher';
import SnapshotInteractiveMode from '../SnapshotInteractiveMode';
class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin {
private _snapshotInteractiveMode: SnapshotInteractiveMode = new SnapshotInteractiveMode(
this._stdout,
);
private _failedSnapshotTestAssertions: Array<AssertionLocation> = [];
isInternal: true = true;
getFailedSnapshotTestAssertions(
testResults: AggregatedResult,
): Array<AssertionLocation> {
const failedTestPaths: Array<AssertionLocation> = [];
if (testResults.numFailedTests === 0 || !testResults.testResults) {
return failedTestPaths;
}
testResults.testResults.forEach(testResult => {
if (testResult.snapshot && testResult.snapshot.unmatched) {
testResult.testResults.forEach(result => {
if (result.status === 'failed') {
failedTestPaths.push({
fullName: result.fullName,
path: testResult.testFilePath,
});
}
});
}
});
return failedTestPaths;
}
apply(hooks: JestHookSubscriber) {
hooks.onTestRunComplete(results => {
this._failedSnapshotTestAssertions = this.getFailedSnapshotTestAssertions(
results,
);
if (this._snapshotInteractiveMode.isActive()) {
this._snapshotInteractiveMode.updateWithResults(results);
}
});
}
onKey(key: string) {
if (this._snapshotInteractiveMode.isActive()) {
this._snapshotInteractiveMode.put(key);
}
}
run(
_globalConfig: Config.GlobalConfig,
updateConfigAndRun: Function,
): Promise<void> {
if (this._failedSnapshotTestAssertions.length) {
return new Promise(res => {
this._snapshotInteractiveMode.run(
this._failedSnapshotTestAssertions,
(assertion, shouldUpdateSnapshot) => {
updateConfigAndRun({
mode: 'watch',
testNamePattern: assertion ? `^${assertion.fullName}$` : '',
testPathPattern: assertion ? assertion.path : '',
updateSnapshot: shouldUpdateSnapshot ? 'all' : 'none',
});
if (!this._snapshotInteractiveMode.isActive()) {
res();
}
},
);
});
} else {
return Promise.resolve();
}
}
getUsageInfo() {
if (
this._failedSnapshotTestAssertions &&
this._failedSnapshotTestAssertions.length > 0
) {
return {
key: 'i',
prompt: 'update failing snapshots interactively',
};
}
return null;
}
}
export default UpdateSnapshotInteractivePlugin;