forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.ts
210 lines (187 loc) Β· 5.9 KB
/
print.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
/**
* 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.
*
*/
import getType, {isPrimitive} from 'jest-get-type';
import {
EXPECTED_COLOR,
INVERTED_COLOR,
RECEIVED_COLOR,
diff,
getLabelPrinter,
printExpected,
printReceived,
stringify,
} from 'jest-matcher-utils';
import {isOneline} from './utils';
// Format substring but do not enclose in double quote marks.
// The replacement is compatible with pretty-format package.
const printSubstring = (val: string): string => val.replace(/"|\\/g, '\\$&');
export const printReceivedStringContainExpectedSubstring = (
received: string,
start: number,
length: number, // not end
): string =>
RECEIVED_COLOR(
'"' +
printSubstring(received.slice(0, start)) +
INVERTED_COLOR(printSubstring(received.slice(start, start + length))) +
printSubstring(received.slice(start + length)) +
'"',
);
export const printReceivedStringContainExpectedResult = (
received: string,
result: RegExpExecArray | null,
): string =>
result === null
? printReceived(received)
: printReceivedStringContainExpectedSubstring(
received,
result.index,
result[0].length,
);
// The serialized array is compatible with pretty-format package min option.
// However, items have default stringify depth (instead of depth - 1)
// so expected item looks consistent by itself and enclosed in the array.
export const printReceivedArrayContainExpectedItem = (
received: Array<unknown>,
index: number,
): string =>
RECEIVED_COLOR(
'[' +
received
.map((item, i) => {
const stringified = stringify(item);
return i === index ? INVERTED_COLOR(stringified) : stringified;
})
.join(', ') +
']',
);
export const printCloseTo = (
receivedDiff: number,
expectedDiff: number,
precision: number,
isNot: boolean,
): string => {
const receivedDiffString = stringify(receivedDiff);
const expectedDiffString = receivedDiffString.includes('e')
? expectedDiff.toExponential(0)
: 0 <= precision && precision <= 20
? expectedDiff.toFixed(precision + 1)
: stringify(expectedDiff);
return (
`Expected precision: ${isNot ? ' ' : ''} ${printExpected(
precision,
)}\n` +
`Expected difference: ${isNot ? 'not ' : ''}< ${EXPECTED_COLOR(
expectedDiffString,
)}\n` +
`Received difference: ${isNot ? ' ' : ''} ${RECEIVED_COLOR(
receivedDiffString,
)}`
);
};
const shouldPrintDiff = (expected: unknown, received: unknown): boolean => {
const expectedType = getType(expected);
const receivedType = getType(received);
if (expectedType !== receivedType) {
return false;
}
if (isPrimitive(expected)) {
// Print diff only if both strings have more than one line.
return expectedType === 'string' && !isOneline(expected, received);
}
if (
expectedType === 'date' ||
expectedType === 'function' ||
expectedType === 'regexp'
) {
return false;
}
if (expected instanceof Error && received instanceof Error) {
return false;
}
return true;
};
export const printDiffOrStringify = (
expected: unknown,
received: unknown,
expectedLabel: string, // include colon and one or more spaces,
receivedLabel: string, // same as returned by getLabelPrinter
expand?: boolean, // diff option: true if `--expand` CLI option
): string => {
// Cannot use same serialization as shortcut to avoid diff,
// because stringify (that is, pretty-format with min option)
// omits constructor name for array or object, too bad so sad :(
const difference = shouldPrintDiff(expected, received)
? diff(expected, received, {
aAnnotation: expectedLabel,
bAnnotation: receivedLabel,
expand,
}) // string | null
: null;
// Cannot reuse value of stringify(received) in report string,
// because printReceived does inverse highlight space at end of line,
// but RECEIVED_COLOR does not (it refers to a plain chalk method).
if (
typeof difference === 'string' &&
difference.includes('- ' + expectedLabel) &&
difference.includes('+ ' + receivedLabel)
) {
return difference;
}
const printLabel = getLabelPrinter(expectedLabel, receivedLabel);
return (
`${printLabel(expectedLabel)}${printExpected(expected)}\n` +
`${printLabel(receivedLabel)}${
stringify(expected) === stringify(received)
? 'serializes to the same string'
: printReceived(received)
}`
);
};
export const printExpectedConstructorName = (
label: string,
expected: Function,
) => printConstructorName(label, expected, false, true) + '\n';
export const printExpectedConstructorNameNot = (
label: string,
expected: Function,
) => printConstructorName(label, expected, true, true) + '\n';
export const printReceivedConstructorName = (
label: string,
received: Function,
) => printConstructorName(label, received, false, false) + '\n';
export function printReceivedConstructorNameNot(
label: string,
received: Function,
expected: Function,
) {
let printed = printConstructorName(label, received, true, false);
if (typeof received.name === 'string' && received.name.length !== 0) {
printed += ` ${
Object.getPrototypeOf(received) === expected
? 'extends'
: 'extends β¦ extends'
} ${EXPECTED_COLOR(expected.name)}`;
}
return printed + '\n';
}
const printConstructorName = (
label: string,
constructor: Function,
isNot: boolean,
isExpected: boolean,
): string =>
typeof constructor.name !== 'string'
? `${label} name is not a string`
: constructor.name.length === 0
? `${label} name is an empty string`
: `${label}: ${!isNot ? '' : isExpected ? 'not ' : ' '}${
isExpected
? EXPECTED_COLOR(constructor.name)
: RECEIVED_COLOR(constructor.name)
}`;