@as-pect/snapshots
Version:
A package for dealing with snapshots
52 lines • 1.99 kB
JavaScript
import { SnapshotDiffResult } from "./SnapshotDiffResult.js";
import { diffLines } from "diff";
export class SnapshotDiff {
left;
right;
results = new Map();
constructor(left, right) {
this.left = left;
this.right = right;
this.calculateDiff();
}
calculateDiff() {
const left = this.left.values;
const right = this.right.values;
// loop over the items on the left side
for (const [key, value] of left.entries()) {
// the snapshot exists, NoChange or Different
if (right.has(key)) {
const rightValue = right.get(key);
const lines = diffLines(rightValue, value);
const result = new SnapshotDiffResult();
result.left = value;
result.right = rightValue;
result.type = value === rightValue ? 0 /* SnapshotDiffResultType.NoChange */ : 3 /* SnapshotDiffResultType.Different */;
result.changes = lines;
this.results.set(key, result);
}
else {
// it was added
const result = new SnapshotDiffResult();
result.left = value;
result.right = null;
result.type = 1 /* SnapshotDiffResultType.Added */;
result.changes = diffLines("", value);
this.results.set(key, result);
}
}
// loop over the items on the right side
for (const [key, value] of right.entries()) {
if (!left.has(key)) {
// it was removed
const result = new SnapshotDiffResult();
result.left = null;
result.right = value;
result.changes = diffLines(value, "");
result.type = 2 /* SnapshotDiffResultType.Removed */;
this.results.set(key, result);
}
}
}
}
//# sourceMappingURL=SnapshotDiff.js.map