/
printSnapshot.ts
346 lines (302 loc) · 9.31 KB
/
printSnapshot.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* 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.
*/
/* eslint-disable local/ban-types-eventually */
import chalk = require('chalk');
// Temporary hack because getObjectSubset has known limitations,
// is not in the public interface of the expect package,
// and the long-term goal is to use a non-serialization diff.
import {getObjectSubset} from 'expect/build/utils';
import {
DIFF_DELETE,
DIFF_EQUAL,
DIFF_INSERT,
Diff,
DiffOptionsColor,
diffLinesUnified,
diffLinesUnified2,
diffStringsRaw,
diffStringsUnified,
} from 'jest-diff';
import getType = require('jest-get-type');
import {
BOLD_WEIGHT,
EXPECTED_COLOR,
INVERTED_COLOR,
MatcherHintOptions,
RECEIVED_COLOR,
getLabelPrinter,
matcherHint,
} from 'jest-matcher-utils';
import prettyFormat from 'pretty-format';
import {
aBackground2,
aBackground3,
aForeground2,
aForeground3,
bBackground2,
bBackground3,
bForeground2,
bForeground3,
} from './colors';
import {dedentLines} from './dedentLines';
import type {MatchSnapshotConfig} from './types';
import {deserializeString, minify, serialize} from './utils';
type Chalk = chalk.Chalk;
export const getSnapshotColorForChalkInstance = (
chalkInstance: Chalk,
): DiffOptionsColor => {
const level = chalkInstance.level;
if (level === 3) {
return chalkInstance
.rgb(aForeground3[0], aForeground3[1], aForeground3[2])
.bgRgb(aBackground3[0], aBackground3[1], aBackground3[2]);
}
if (level === 2) {
return chalkInstance.ansi256(aForeground2).bgAnsi256(aBackground2);
}
return chalkInstance.magenta.bgYellowBright;
};
export const getReceivedColorForChalkInstance = (
chalkInstance: Chalk,
): DiffOptionsColor => {
const level = chalkInstance.level;
if (level === 3) {
return chalkInstance
.rgb(bForeground3[0], bForeground3[1], bForeground3[2])
.bgRgb(bBackground3[0], bBackground3[1], bBackground3[2]);
}
if (level === 2) {
return chalkInstance.ansi256(bForeground2).bgAnsi256(bBackground2);
}
return chalkInstance.cyan.bgWhiteBright; // also known as teal
};
export const aSnapshotColor = getSnapshotColorForChalkInstance(chalk);
export const bReceivedColor = getReceivedColorForChalkInstance(chalk);
export const noColor = (string: string): string => string;
export const HINT_ARG = 'hint';
export const SNAPSHOT_ARG = 'snapshot';
export const PROPERTIES_ARG = 'properties';
export const matcherHintFromConfig = (
{
context: {isNot, promise},
hint,
inlineSnapshot,
matcherName,
properties,
}: MatchSnapshotConfig,
isUpdatable: boolean,
): string => {
const options: MatcherHintOptions = {isNot, promise};
if (isUpdatable) {
options.receivedColor = bReceivedColor;
}
let expectedArgument = '';
if (typeof properties === 'object') {
expectedArgument = PROPERTIES_ARG;
if (isUpdatable) {
options.expectedColor = noColor;
}
if (typeof hint === 'string' && hint.length !== 0) {
options.secondArgument = HINT_ARG;
options.secondArgumentColor = BOLD_WEIGHT;
} else if (typeof inlineSnapshot === 'string') {
options.secondArgument = SNAPSHOT_ARG;
if (isUpdatable) {
options.secondArgumentColor = aSnapshotColor;
} else {
options.secondArgumentColor = noColor;
}
}
} else {
if (typeof hint === 'string' && hint.length !== 0) {
expectedArgument = HINT_ARG;
options.expectedColor = BOLD_WEIGHT;
} else if (typeof inlineSnapshot === 'string') {
expectedArgument = SNAPSHOT_ARG;
if (isUpdatable) {
options.expectedColor = aSnapshotColor;
}
}
}
return matcherHint(matcherName, undefined, expectedArgument, options);
};
// Given array of diffs, return string:
// * include common substrings
// * exclude change substrings which have opposite op
// * include change substrings which have argument op
// with change color only if there is a common substring
const joinDiffs = (
diffs: Array<Diff>,
op: number,
hasCommon: boolean,
): string =>
diffs.reduce(
(reduced: string, diff: Diff): string =>
reduced +
(diff[0] === DIFF_EQUAL
? diff[1]
: diff[0] !== op
? ''
: hasCommon
? INVERTED_COLOR(diff[1])
: diff[1]),
'',
);
const isLineDiffable = (received: unknown): boolean => {
const receivedType = getType(received);
if (getType.isPrimitive(received)) {
return typeof received === 'string';
}
if (
receivedType === 'date' ||
receivedType === 'function' ||
receivedType === 'regexp'
) {
return false;
}
if (received instanceof Error) {
return false;
}
if (
receivedType === 'object' &&
typeof (received as any).asymmetricMatch === 'function'
) {
return false;
}
return true;
};
export const printExpected = (val: unknown): string =>
EXPECTED_COLOR(minify(val));
export const printReceived = (val: unknown): string =>
RECEIVED_COLOR(minify(val));
export const printPropertiesAndReceived = (
properties: object,
received: object,
expand: boolean, // CLI options: true if `--expand` or false if `--no-expand`
): string => {
const aAnnotation = 'Expected properties';
const bAnnotation = 'Received value';
if (isLineDiffable(properties) && isLineDiffable(received)) {
return diffLinesUnified(
serialize(properties).split('\n'),
serialize(getObjectSubset(received, properties)).split('\n'),
{
aAnnotation,
aColor: EXPECTED_COLOR,
bAnnotation,
bColor: RECEIVED_COLOR,
changeLineTrailingSpaceColor: chalk.bgYellow,
commonLineTrailingSpaceColor: chalk.bgYellow,
emptyFirstOrLastLinePlaceholder: '↵', // U+21B5
expand,
includeChangeCounts: true,
},
);
}
const printLabel = getLabelPrinter(aAnnotation, bAnnotation);
return (
printLabel(aAnnotation) +
printExpected(properties) +
'\n' +
printLabel(bAnnotation) +
printReceived(received)
);
};
const MAX_DIFF_STRING_LENGTH = 20000;
export const printSnapshotAndReceived = (
a: string, // snapshot without extra line breaks
b: string, // received serialized but without extra line breaks
received: unknown,
expand: boolean, // CLI options: true if `--expand` or false if `--no-expand`
): string => {
const aAnnotation = 'Snapshot';
const bAnnotation = 'Received';
const aColor = aSnapshotColor;
const bColor = bReceivedColor;
const options = {
aAnnotation,
aColor,
bAnnotation,
bColor,
changeLineTrailingSpaceColor: noColor,
commonLineTrailingSpaceColor: chalk.bgYellow,
emptyFirstOrLastLinePlaceholder: '↵', // U+21B5
expand,
includeChangeCounts: true,
};
if (typeof received === 'string') {
if (
a.length >= 2 &&
a.startsWith('"') &&
a.endsWith('"') &&
b === prettyFormat(received)
) {
// If snapshot looks like default serialization of a string
// and received is string which has default serialization.
if (!a.includes('\n') && !b.includes('\n')) {
// If neither string is multiline,
// display as labels and quoted strings.
let aQuoted = a;
let bQuoted = b;
if (
a.length - 2 <= MAX_DIFF_STRING_LENGTH &&
b.length - 2 <= MAX_DIFF_STRING_LENGTH
) {
const diffs = diffStringsRaw(a.slice(1, -1), b.slice(1, -1), true);
const hasCommon = diffs.some(diff => diff[0] === DIFF_EQUAL);
aQuoted = '"' + joinDiffs(diffs, DIFF_DELETE, hasCommon) + '"';
bQuoted = '"' + joinDiffs(diffs, DIFF_INSERT, hasCommon) + '"';
}
const printLabel = getLabelPrinter(aAnnotation, bAnnotation);
return (
printLabel(aAnnotation) +
aColor(aQuoted) +
'\n' +
printLabel(bAnnotation) +
bColor(bQuoted)
);
}
// Else either string is multiline, so display as unquoted strings.
a = deserializeString(a); // hypothetical expected string
b = received; // not serialized
}
// Else expected had custom serialization or was not a string
// or received has custom serialization.
return a.length <= MAX_DIFF_STRING_LENGTH &&
b.length <= MAX_DIFF_STRING_LENGTH
? diffStringsUnified(a, b, options)
: diffLinesUnified(a.split('\n'), b.split('\n'), options);
}
if (isLineDiffable(received)) {
const aLines2 = a.split('\n');
const bLines2 = b.split('\n');
// Fall through to fix a regression for custom serializers
// like jest-snapshot-serializer-raw that ignore the indent option.
const b0 = serialize(received, 0);
if (b0 !== b) {
const aLines0 = dedentLines(aLines2);
if (aLines0 !== null) {
// Compare lines without indentation.
const bLines0 = b0.split('\n');
return diffLinesUnified2(aLines2, bLines2, aLines0, bLines0, options);
}
}
// Fall back because:
// * props include a multiline string
// * text has more than one adjacent line
// * markup does not close
return diffLinesUnified(aLines2, bLines2, options);
}
const printLabel = getLabelPrinter(aAnnotation, bAnnotation);
return (
printLabel(aAnnotation) +
aColor(a) +
'\n' +
printLabel(bAnnotation) +
bColor(b)
);
};