-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathindex.ts
388 lines (345 loc) · 11.3 KB
/
index.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// This is a fork of Jest's jest-diff package, but it doesn't depend on Node environment (like chalk).
import type { PrettyFormatOptions } from '@vitest/pretty-format'
import type { DiffOptions } from './types'
import {
format as prettyFormat,
plugins as prettyFormatPlugins,
} from '@vitest/pretty-format'
import c from 'tinyrainbow'
import { stringify } from '../display'
import { deepClone, getOwnProperties, getType as getSimpleType } from '../helpers'
import { Diff, DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT } from './cleanupSemantic'
import { NO_DIFF_MESSAGE, SIMILAR_MESSAGE } from './constants'
import { diffLinesRaw, diffLinesUnified, diffLinesUnified2 } from './diffLines'
import { getType } from './getType'
import { normalizeDiffOptions } from './normalizeDiffOptions'
import { diffStringsRaw, diffStringsUnified } from './printDiffs'
export type { DiffOptions, DiffOptionsColor, SerializedDiffOptions } from './types'
export { diffLinesRaw, diffLinesUnified, diffLinesUnified2 }
export { diffStringsRaw, diffStringsUnified }
export { Diff, DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT }
function getCommonMessage(message: string, options?: DiffOptions) {
const { commonColor } = normalizeDiffOptions(options)
return commonColor(message)
}
const {
AsymmetricMatcher,
DOMCollection,
DOMElement,
Immutable,
ReactElement,
ReactTestComponent,
} = prettyFormatPlugins
const PLUGINS = [
ReactTestComponent,
ReactElement,
DOMElement,
DOMCollection,
Immutable,
AsymmetricMatcher,
prettyFormatPlugins.Error,
]
const FORMAT_OPTIONS = {
plugins: PLUGINS,
}
const FALLBACK_FORMAT_OPTIONS = {
callToJSON: false,
maxDepth: 10,
plugins: PLUGINS,
}
// Generate a string that will highlight the difference between two values
// with green and red. (similar to how github does code diffing)
/**
* @param a Expected value
* @param b Received value
* @param options Diff options
* @returns {string | null} a string diff
*/
export function diff(a: any, b: any, options?: DiffOptions): string | undefined {
if (Object.is(a, b)) {
return ''
}
const aType = getType(a)
let expectedType = aType
let omitDifference = false
if (aType === 'object' && typeof a.asymmetricMatch === 'function') {
if (a.$$typeof !== Symbol.for('jest.asymmetricMatcher')) {
// Do not know expected type of user-defined asymmetric matcher.
return undefined
}
if (typeof a.getExpectedType !== 'function') {
// For example, expect.anything() matches either null or undefined
return undefined
}
expectedType = a.getExpectedType()
// Primitive types boolean and number omit difference below.
// For example, omit difference for expect.stringMatching(regexp)
omitDifference = expectedType === 'string'
}
if (expectedType !== getType(b)) {
const { aAnnotation, aColor, aIndicator, bAnnotation, bColor, bIndicator }
= normalizeDiffOptions(options)
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options)
const aDisplay = prettyFormat(a, formatOptions)
const bDisplay = prettyFormat(b, formatOptions)
const aDiff = `${aColor(`${aIndicator} ${aAnnotation}:`)} \n${aDisplay}`
const bDiff = `${bColor(`${bIndicator} ${bAnnotation}:`)} \n${bDisplay}`
return `${aDiff}\n\n${bDiff}`
}
if (omitDifference) {
return undefined
}
switch (aType) {
case 'string':
return diffLinesUnified(a.split('\n'), b.split('\n'), options)
case 'boolean':
case 'number':
return comparePrimitive(a, b, options)
case 'map':
return compareObjects(sortMap(a), sortMap(b), options)
case 'set':
return compareObjects(sortSet(a), sortSet(b), options)
default:
return compareObjects(a, b, options)
}
}
function comparePrimitive(
a: number | boolean,
b: number | boolean,
options?: DiffOptions,
) {
const aFormat = prettyFormat(a, FORMAT_OPTIONS)
const bFormat = prettyFormat(b, FORMAT_OPTIONS)
return aFormat === bFormat
? ''
: diffLinesUnified(aFormat.split('\n'), bFormat.split('\n'), options)
}
function sortMap(map: Map<unknown, unknown>) {
return new Map(Array.from(map.entries()).sort())
}
function sortSet(set: Set<unknown>) {
return new Set(Array.from(set.values()).sort())
}
function compareObjects(
a: Record<string, any>,
b: Record<string, any>,
options?: DiffOptions,
) {
let difference
let hasThrown = false
try {
const formatOptions = getFormatOptions(FORMAT_OPTIONS, options)
difference = getObjectsDifference(a, b, formatOptions, options)
}
catch {
hasThrown = true
}
const noDiffMessage = getCommonMessage(NO_DIFF_MESSAGE, options)
// If the comparison yields no results, compare again but this time
// without calling `toJSON`. It's also possible that toJSON might throw.
if (difference === undefined || difference === noDiffMessage) {
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options)
difference = getObjectsDifference(a, b, formatOptions, options)
if (difference !== noDiffMessage && !hasThrown) {
difference = `${getCommonMessage(
SIMILAR_MESSAGE,
options,
)}\n\n${difference}`
}
}
return difference
}
function getFormatOptions(
formatOptions: PrettyFormatOptions,
options?: DiffOptions,
): PrettyFormatOptions {
const { compareKeys, printBasicPrototype } = normalizeDiffOptions(options)
return {
...formatOptions,
compareKeys,
printBasicPrototype,
}
}
function getObjectsDifference(
a: Record<string, any>,
b: Record<string, any>,
formatOptions: PrettyFormatOptions,
options?: DiffOptions,
): string {
const formatOptionsZeroIndent = { ...formatOptions, indent: 0 }
const aCompare = prettyFormat(a, formatOptionsZeroIndent)
const bCompare = prettyFormat(b, formatOptionsZeroIndent)
if (aCompare === bCompare) {
return getCommonMessage(NO_DIFF_MESSAGE, options)
}
else {
const aDisplay = prettyFormat(a, formatOptions)
const bDisplay = prettyFormat(b, formatOptions)
return diffLinesUnified2(
aDisplay.split('\n'),
bDisplay.split('\n'),
aCompare.split('\n'),
bCompare.split('\n'),
options,
)
}
}
const MAX_DIFF_STRING_LENGTH = 20_000
function isAsymmetricMatcher(data: any) {
const type = getSimpleType(data)
return type === 'Object' && typeof data.asymmetricMatch === 'function'
}
function isReplaceable(obj1: any, obj2: any) {
const obj1Type = getSimpleType(obj1)
const obj2Type = getSimpleType(obj2)
return (
obj1Type === obj2Type && (obj1Type === 'Object' || obj1Type === 'Array')
)
}
export function printDiffOrStringify(
expected: unknown,
received: unknown,
options?: DiffOptions,
): string | undefined {
const { aAnnotation, bAnnotation } = normalizeDiffOptions(options)
if (
typeof expected === 'string'
&& typeof received === 'string'
&& expected.length > 0
&& received.length > 0
&& expected.length <= MAX_DIFF_STRING_LENGTH
&& received.length <= MAX_DIFF_STRING_LENGTH
&& expected !== received
) {
if (expected.includes('\n') || received.includes('\n')) {
return diffStringsUnified(received, expected, options)
}
const [diffs] = diffStringsRaw(received, expected, true)
const hasCommonDiff = diffs.some(diff => diff[0] === DIFF_EQUAL)
const printLabel = getLabelPrinter(aAnnotation, bAnnotation)
const expectedLine
= printLabel(aAnnotation)
+ printExpected(
getCommonAndChangedSubstrings(diffs, DIFF_DELETE, hasCommonDiff),
)
const receivedLine
= printLabel(bAnnotation)
+ printReceived(
getCommonAndChangedSubstrings(diffs, DIFF_INSERT, hasCommonDiff),
)
return `${expectedLine}\n${receivedLine}`
}
// if (isLineDiffable(expected, received)) {
const clonedExpected = deepClone(expected, { forceWritable: true })
const clonedReceived = deepClone(received, { forceWritable: true })
const { replacedExpected, replacedActual } = replaceAsymmetricMatcher(clonedExpected, clonedReceived)
const difference = diff(replacedExpected, replacedActual, options)
return difference
// }
// const printLabel = getLabelPrinter(aAnnotation, bAnnotation)
// const expectedLine = printLabel(aAnnotation) + printExpected(expected)
// const receivedLine
// = printLabel(bAnnotation)
// + (stringify(expected) === stringify(received)
// ? 'serializes to the same string'
// : printReceived(received))
// return `${expectedLine}\n${receivedLine}`
}
export function replaceAsymmetricMatcher(
actual: any,
expected: any,
actualReplaced: WeakSet<WeakKey> = new WeakSet(),
expectedReplaced: WeakSet<WeakKey> = new WeakSet(),
): {
replacedActual: any
replacedExpected: any
} {
// handle asymmetric Error.cause diff
if (
actual instanceof Error
&& expected instanceof Error
&& typeof actual.cause !== 'undefined'
&& typeof expected.cause === 'undefined'
) {
delete actual.cause
return {
replacedActual: actual,
replacedExpected: expected,
}
}
if (!isReplaceable(actual, expected)) {
return { replacedActual: actual, replacedExpected: expected }
}
if (actualReplaced.has(actual) || expectedReplaced.has(expected)) {
return { replacedActual: actual, replacedExpected: expected }
}
actualReplaced.add(actual)
expectedReplaced.add(expected)
getOwnProperties(expected).forEach((key) => {
const expectedValue = expected[key]
const actualValue = actual[key]
if (isAsymmetricMatcher(expectedValue)) {
if (expectedValue.asymmetricMatch(actualValue)) {
actual[key] = expectedValue
}
}
else if (isAsymmetricMatcher(actualValue)) {
if (actualValue.asymmetricMatch(expectedValue)) {
expected[key] = actualValue
}
}
else if (isReplaceable(actualValue, expectedValue)) {
const replaced = replaceAsymmetricMatcher(
actualValue,
expectedValue,
actualReplaced,
expectedReplaced,
)
actual[key] = replaced.replacedActual
expected[key] = replaced.replacedExpected
}
})
return {
replacedActual: actual,
replacedExpected: expected,
}
}
type PrintLabel = (string: string) => string
export function getLabelPrinter(...strings: Array<string>): PrintLabel {
const maxLength = strings.reduce(
(max, string) => (string.length > max ? string.length : max),
0,
)
return (string: string): string =>
`${string}: ${' '.repeat(maxLength - string.length)}`
}
const SPACE_SYMBOL = '\u{00B7}' // middle dot
function replaceTrailingSpaces(text: string): string {
return text.replace(/\s+$/gm, spaces => SPACE_SYMBOL.repeat(spaces.length))
}
function printReceived(object: unknown): string {
return c.red(replaceTrailingSpaces(stringify(object)))
}
function printExpected(value: unknown): string {
return c.green(replaceTrailingSpaces(stringify(value)))
}
function getCommonAndChangedSubstrings(diffs: Array<Diff>, op: number, hasCommonDiff: boolean): string {
return diffs.reduce(
(reduced: string, diff: Diff): string =>
reduced
+ (diff[0] === DIFF_EQUAL
? diff[1]
: diff[0] === op
? hasCommonDiff
? c.inverse(diff[1])
: diff[1]
: ''),
'',
)
}