File size: 1,948 Bytes
bc20498 |
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 |
import { join, dirname, basename, isAbsolute, resolve } from 'pathe';
class SnapshotManager {
constructor(options) {
this.options = options;
this.clear();
}
summary = void 0;
extension = ".snap";
clear() {
this.summary = emptySummary(this.options);
}
add(result) {
addSnapshotResult(this.summary, result);
}
resolvePath(testPath) {
const resolver = this.options.resolveSnapshotPath || (() => {
return join(
join(
dirname(testPath),
"__snapshots__"
),
`${basename(testPath)}${this.extension}`
);
});
const path = resolver(testPath, this.extension);
return path;
}
resolveRawPath(testPath, rawPath) {
return isAbsolute(rawPath) ? rawPath : resolve(dirname(testPath), rawPath);
}
}
function emptySummary(options) {
const summary = {
added: 0,
failure: false,
filesAdded: 0,
filesRemoved: 0,
filesRemovedList: [],
filesUnmatched: 0,
filesUpdated: 0,
matched: 0,
total: 0,
unchecked: 0,
uncheckedKeysByFile: [],
unmatched: 0,
updated: 0,
didUpdate: options.updateSnapshot === "all"
};
return summary;
}
function addSnapshotResult(summary, result) {
if (result.added)
summary.filesAdded++;
if (result.fileDeleted)
summary.filesRemoved++;
if (result.unmatched)
summary.filesUnmatched++;
if (result.updated)
summary.filesUpdated++;
summary.added += result.added;
summary.matched += result.matched;
summary.unchecked += result.unchecked;
if (result.uncheckedKeys && result.uncheckedKeys.length > 0) {
summary.uncheckedKeysByFile.push({
filePath: result.filepath,
keys: result.uncheckedKeys
});
}
summary.unmatched += result.unmatched;
summary.updated += result.updated;
summary.total += result.added + result.matched + result.unmatched + result.updated;
}
export { SnapshotManager, addSnapshotResult, emptySummary };
|