/
jest-matcher-utils.ts
110 lines (92 loc) · 2.84 KB
/
jest-matcher-utils.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
import { getColors, stringify } from '@vitest/utils'
import { unifiedDiff } from '@vitest/utils/diff'
import type { DiffOptions, MatcherHintOptions } from './types'
export { stringify }
export function getMatcherUtils() {
const c = () => getColors()
const EXPECTED_COLOR = c().green
const RECEIVED_COLOR = c().red
const INVERTED_COLOR = c().inverse
const BOLD_WEIGHT = c().bold
const DIM_COLOR = c().dim
function matcherHint(
matcherName: string,
received = 'received',
expected = 'expected',
options: MatcherHintOptions = {},
) {
const {
comment = '',
isDirectExpectCall = false, // seems redundant with received === ''
isNot = false,
promise = '',
secondArgument = '',
expectedColor = EXPECTED_COLOR,
receivedColor = RECEIVED_COLOR,
secondArgumentColor = EXPECTED_COLOR,
} = options
let hint = ''
let dimString = 'expect' // concatenate adjacent dim substrings
if (!isDirectExpectCall && received !== '') {
hint += DIM_COLOR(`${dimString}(`) + receivedColor(received)
dimString = ')'
}
if (promise !== '') {
hint += DIM_COLOR(`${dimString}.`) + promise
dimString = ''
}
if (isNot) {
hint += `${DIM_COLOR(`${dimString}.`)}not`
dimString = ''
}
if (matcherName.includes('.')) {
// Old format: for backward compatibility,
// especially without promise or isNot options
dimString += matcherName
}
else {
// New format: omit period from matcherName arg
hint += DIM_COLOR(`${dimString}.`) + matcherName
dimString = ''
}
if (expected === '') {
dimString += '()'
}
else {
hint += DIM_COLOR(`${dimString}(`) + expectedColor(expected)
if (secondArgument)
hint += DIM_COLOR(', ') + secondArgumentColor(secondArgument)
dimString = ')'
}
if (comment !== '')
dimString += ` // ${comment}`
if (dimString !== '')
hint += DIM_COLOR(dimString)
return hint
}
const SPACE_SYMBOL = '\u{00B7}' // middle dot
// Instead of inverse highlight which now implies a change,
// replace common spaces with middle dot at the end of any line.
const replaceTrailingSpaces = (text: string): string =>
text.replace(/\s+$/gm, spaces => SPACE_SYMBOL.repeat(spaces.length))
const printReceived = (object: unknown): string =>
RECEIVED_COLOR(replaceTrailingSpaces(stringify(object)))
const printExpected = (value: unknown): string =>
EXPECTED_COLOR(replaceTrailingSpaces(stringify(value)))
return {
EXPECTED_COLOR,
RECEIVED_COLOR,
INVERTED_COLOR,
BOLD_WEIGHT,
DIM_COLOR,
matcherHint,
printReceived,
printExpected,
}
}
// TODO: do something with options
export function diff(a: any, b: any, options?: DiffOptions) {
return unifiedDiff(b, a, {
showLegend: options?.showLegend,
})
}