|
1 |
| -import * as diff from 'diff' |
2 |
| - |
3 |
| -export interface DiffOptions { |
4 |
| - outputTruncateLength?: number |
5 |
| - outputDiffLines?: number |
6 |
| - showLegend?: boolean |
7 |
| -} |
8 |
| - |
9 |
| -function formatLine(line: string, maxWidth: number) { |
10 |
| - return line.slice(0, maxWidth) + (line.length > maxWidth ? '…' : '') |
11 |
| -} |
12 |
| - |
13 |
| -export function unifiedDiff(actual: string, expected: string, options: DiffOptions = {}) { |
14 |
| - if (actual === expected) |
15 |
| - return '' |
16 |
| - |
17 |
| - const { outputTruncateLength = 80, outputDiffLines, showLegend = true } = options |
18 |
| - |
19 |
| - const indent = ' ' |
20 |
| - const diffLimit = outputDiffLines || 15 |
21 |
| - |
22 |
| - const counts = { |
23 |
| - '+': 0, |
24 |
| - '-': 0, |
25 |
| - } |
26 |
| - let previousState: '-' | '+' | null = null |
27 |
| - let previousCount = 0 |
28 |
| - function preprocess(line: string) { |
29 |
| - if (!line || line.match(/\\ No newline/)) |
30 |
| - return |
31 |
| - |
32 |
| - const char = line[0] as '+' | '-' |
33 |
| - if ('-+'.includes(char)) { |
34 |
| - if (previousState !== char) { |
35 |
| - previousState = char |
36 |
| - previousCount = 0 |
37 |
| - } |
38 |
| - previousCount++ |
39 |
| - counts[char]++ |
40 |
| - if (previousCount === diffLimit) |
41 |
| - return `${char} ...` |
42 |
| - else if (previousCount > diffLimit) |
43 |
| - return |
44 |
| - } |
45 |
| - return line |
46 |
| - } |
47 |
| - |
48 |
| - const msg = diff.createPatch('string', expected, actual) |
49 |
| - const lines = msg.split('\n').slice(5).map(preprocess).filter(Boolean) as string[] |
50 |
| - const isCompact = counts['+'] === 1 && counts['-'] === 1 && lines.length === 2 |
51 |
| - |
52 |
| - let formatted = lines.map((line: string) => { |
53 |
| - line = line.replace(/\\"/g, '"') |
54 |
| - if (line[0] === '-') { |
55 |
| - line = formatLine(line.slice(1), outputTruncateLength) |
56 |
| - if (isCompact) |
57 |
| - return line |
58 |
| - return `- ${formatLine(line, outputTruncateLength)}` |
59 |
| - } |
60 |
| - if (line[0] === '+') { |
61 |
| - line = formatLine(line.slice(1), outputTruncateLength) |
62 |
| - if (isCompact) |
63 |
| - return line |
64 |
| - return `+ ${formatLine(line, outputTruncateLength)}` |
65 |
| - } |
66 |
| - if (line.match(/@@/)) |
67 |
| - return '--' |
68 |
| - return ` ${line}` |
69 |
| - }) |
70 |
| - |
71 |
| - if (showLegend) { |
72 |
| - // Compact mode |
73 |
| - if (isCompact) { |
74 |
| - formatted = [ |
75 |
| - `- Expected ${formatted[0]}`, |
76 |
| - `+ Received ${formatted[1]}`, |
77 |
| - ] |
78 |
| - } |
79 |
| - else { |
80 |
| - if (formatted[0].includes('"')) |
81 |
| - formatted[0] = formatted[0].replace('"', '') |
82 |
| - |
83 |
| - const last = formatted.length - 1 |
84 |
| - if (formatted[last].endsWith('"')) |
85 |
| - formatted[last] = formatted[last].slice(0, formatted[last].length - 1) |
86 |
| - |
87 |
| - formatted.unshift( |
88 |
| - `- Expected - ${counts['-']}`, |
89 |
| - `+ Received + ${counts['+']}`, |
90 |
| - '', |
91 |
| - ) |
92 |
| - } |
93 |
| - } |
94 |
| - |
95 |
| - return formatted.map(i => indent + i).join('\n') |
96 |
| -} |
| 1 | +export { unifiedDiff } from '@vitest/utils/diff' |
0 commit comments