Skip to content

Commit ea1ccd0

Browse files
cdervclaude
andcommitted
tests, snapshot - improve diff output for snapshot comparison failures
- Show unified diff with colored lines and word-level changes with context instead of dumping full file contents - Use character diff for whitespace-only changes with visible markers (⏎ for newlines, → for tabs, · for spaces) - Save diff to .diff file on failure, clean up when snapshot passes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c9cfe4c commit ea1ccd0

2 files changed

Lines changed: 104 additions & 5 deletions

File tree

‎tests/verify-snapshot.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { extname } from "../src/deno_ral/path.ts";
88
import { normalizeNewlines } from "../src/core/text.ts";
99
import { withDocxContent } from "./verify.ts";
10+
import { createPatch, diffWordsWithSpace, diffChars } from "../src/core/lib/external/diff.js";
1011

1112
import * as slimdom from "slimdom";
1213
import xpath from "fontoxpath";
@@ -57,3 +58,31 @@ export const checkSnapshot = async (file: string) => {
5758
const snapshotCanonical = await canonicalizeSnapshot(file + ".snapshot");
5859
return outputCanonical === snapshotCanonical;
5960
}
61+
62+
export const generateSnapshotDiff = async (file: string): Promise<string> => {
63+
const outputCanonical = await canonicalizeSnapshot(file);
64+
const snapshotCanonical = await canonicalizeSnapshot(file + ".snapshot");
65+
return createPatch(
66+
file + ".snapshot",
67+
snapshotCanonical,
68+
outputCanonical,
69+
"expected",
70+
"actual"
71+
);
72+
}
73+
74+
export type WordDiffPart = { value: string; added?: boolean; removed?: boolean };
75+
76+
export const generateInlineDiff = async (file: string): Promise<WordDiffPart[]> => {
77+
const outputCanonical = await canonicalizeSnapshot(file);
78+
const snapshotCanonical = await canonicalizeSnapshot(file + ".snapshot");
79+
80+
const stripWhitespace = (s: string) => s.replace(/\s+/g, "");
81+
const isWhitespaceOnlyChange = stripWhitespace(outputCanonical) === stripWhitespace(snapshotCanonical);
82+
83+
if (isWhitespaceOnlyChange) {
84+
return diffChars(snapshotCanonical, outputCanonical);
85+
}
86+
87+
return diffWordsWithSpace(snapshotCanonical, outputCanonical);
88+
}

‎tests/verify.ts‎

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { unzip } from "../src/core/zip.ts";
2020
import { dirAndStem, safeRemoveSync, which } from "../src/core/path.ts";
2121
import { isWindows } from "../src/deno_ral/platform.ts";
2222
import { execProcess, ExecProcessOptions } from "../src/core/process.ts";
23-
import { canonicalizeSnapshot, checkSnapshot } from "./verify-snapshot.ts";
23+
import { checkSnapshot, generateSnapshotDiff, generateInlineDiff, WordDiffPart } from "./verify-snapshot.ts";
24+
import * as colors from "fmt/colors";
2425

2526
export const withDocxContent = async <T>(
2627
file: string,
@@ -440,18 +441,87 @@ export const ensureHtmlElementCount = (
440441
};
441442
};
442443

444+
const printColoredDiff = (diff: string) => {
445+
for (const line of diff.split("\n")) {
446+
if (line.startsWith("+") && !line.startsWith("+++")) {
447+
console.log(colors.green(line));
448+
} else if (line.startsWith("-") && !line.startsWith("---")) {
449+
console.log(colors.red(line));
450+
} else if (line.startsWith("@@")) {
451+
console.log(colors.dim(line));
452+
} else {
453+
console.log(line);
454+
}
455+
}
456+
};
457+
458+
const escapeWhitespace = (s: string): string => {
459+
return s.replace(/\n/g, "⏎\\n").replace(/\t/g, "→\\t").replace(/ /g, "·");
460+
};
461+
462+
const printCompactInlineDiff = (parts: WordDiffPart[]) => {
463+
const chunks: string[] = [];
464+
let currentChunk = "";
465+
let hasChanges = false;
466+
467+
for (let i = 0; i < parts.length; i++) {
468+
const part = parts[i];
469+
if (part.added || part.removed) {
470+
hasChanges = true;
471+
const displayValue = /^\s+$/.test(part.value) ? escapeWhitespace(part.value) : part.value;
472+
if (part.added) {
473+
currentChunk += colors.bgGreen(colors.black(displayValue));
474+
} else {
475+
currentChunk += colors.bgRed(colors.white(displayValue));
476+
}
477+
} else {
478+
if (hasChanges) {
479+
const contextBefore = part.value.slice(0, 40);
480+
chunks.push(currentChunk + colors.dim(contextBefore + (part.value.length > 40 ? "..." : "")));
481+
currentChunk = "";
482+
hasChanges = false;
483+
}
484+
const nextHasChange = parts.slice(i + 1).some(p => p.added || p.removed);
485+
if (nextHasChange) {
486+
const contextAfter = part.value.slice(-40);
487+
currentChunk = colors.dim((part.value.length > 40 ? "..." : "") + contextAfter);
488+
}
489+
}
490+
}
491+
if (currentChunk) {
492+
chunks.push(currentChunk);
493+
}
494+
495+
for (const chunk of chunks) {
496+
console.log(chunk);
497+
console.log("");
498+
}
499+
};
500+
443501
export const ensureSnapshotMatches = (
444502
file: string,
445503
): Verify => {
446504
return {
447505
name: "Inspecting Snapshot",
448506
verify: async (_output: ExecuteOutput[]) => {
449507
const good = await checkSnapshot(file);
508+
const diffFile = file + ".diff";
450509
if (!good) {
451-
console.log("output:");
452-
console.log(await canonicalizeSnapshot(file));
453-
console.log("snapshot:");
454-
console.log(await canonicalizeSnapshot(file + ".snapshot"));
510+
const diff = await generateSnapshotDiff(file);
511+
const inlineParts = await generateInlineDiff(file);
512+
513+
await Deno.writeTextFile(diffFile, diff);
514+
console.log(`\nDiff saved to: ${diffFile}`);
515+
516+
console.log("\n--- Unified Diff ---");
517+
printColoredDiff(diff);
518+
console.log("--- End Unified Diff ---\n");
519+
520+
console.log("--- Word-level Changes (with context) ---");
521+
printCompactInlineDiff(inlineParts);
522+
console.log("--- End Word-level Changes ---\n");
523+
} else {
524+
safeRemoveSync(diffFile);
455525
}
456526
assert(
457527
good,

0 commit comments

Comments
 (0)