forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
184 lines (166 loc) · 4.46 KB
/
index.js
File metadata and controls
184 lines (166 loc) · 4.46 KB
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
/**
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
import chalk from 'chalk';
import getType from 'jest-get-type';
import prettyFormat from 'pretty-format';
const {
AsymmetricMatcher,
HTMLElement,
Immutable,
ReactElement,
ReactTestComponent,
} = prettyFormat.plugins;
const PLUGINS = [
ReactTestComponent,
ReactElement,
HTMLElement,
Immutable,
AsymmetricMatcher,
];
const EXPECTED_COLOR = chalk.green;
const EXPECTED_BG = chalk.bgGreen;
const RECEIVED_COLOR = chalk.red;
const RECEIVED_BG = chalk.bgRed;
const NUMBERS = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
];
const SUGGEST_TO_EQUAL = chalk.dim(
'Looks like you wanted to test for object/array equality with strict `toBe` matcher. You probably need to use `toEqual` instead.',
);
const stringify = (object: any, maxDepth?: number = 10): string => {
const MAX_LENGTH = 10000;
let result;
try {
result = prettyFormat(object, {
maxDepth,
min: true,
plugins: PLUGINS,
});
} catch (e) {
result = prettyFormat(object, {
callToJSON: false,
maxDepth,
min: true,
plugins: PLUGINS,
});
}
return result.length >= MAX_LENGTH && maxDepth > 1
? stringify(object, Math.floor(maxDepth / 2))
: result;
};
const highlightTrailingWhitespace = (text: string, bgColor: Function): string =>
text.replace(/\s+$/gm, bgColor('$&'));
const printReceived = (object: any) =>
highlightTrailingWhitespace(RECEIVED_COLOR(stringify(object)), RECEIVED_BG);
const printExpected = (value: any) =>
highlightTrailingWhitespace(EXPECTED_COLOR(stringify(value)), EXPECTED_BG);
const printWithType = (
name: string,
received: any,
print: (value: any) => string,
) => {
const type = getType(received);
return (
name +
':' +
(type !== 'null' && type !== 'undefined' ? '\n ' + type + ': ' : ' ') +
print(received)
);
};
const ensureNoExpected = (expected: any, matcherName: string) => {
matcherName || (matcherName = 'This');
if (typeof expected !== 'undefined') {
throw new Error(
matcherHint('[.not]' + matcherName, undefined, '') +
'\n\n' +
'Matcher does not accept any arguments.\n' +
printWithType('Got', expected, printExpected),
);
}
};
const ensureActualIsNumber = (actual: any, matcherName: string) => {
matcherName || (matcherName = 'This matcher');
if (typeof actual !== 'number') {
throw new Error(
matcherHint('[.not]' + matcherName) +
'\n\n' +
`Received value must be a number.\n` +
printWithType('Received', actual, printReceived),
);
}
};
const ensureExpectedIsNumber = (expected: any, matcherName: string) => {
matcherName || (matcherName = 'This matcher');
if (typeof expected !== 'number') {
throw new Error(
matcherHint('[.not]' + matcherName) +
'\n\n' +
`Expected value must be a number.\n` +
printWithType('Got', expected, printExpected),
);
}
};
const ensureNumbers = (actual: any, expected: any, matcherName: string) => {
ensureActualIsNumber(actual, matcherName);
ensureExpectedIsNumber(expected, matcherName);
};
const pluralize = (word: string, count: number) =>
(NUMBERS[count] || count) + ' ' + word + (count === 1 ? '' : 's');
const matcherHint = (
matcherName: string,
received: string = 'received',
expected: string = 'expected',
options: ?{
secondArgument?: ?string,
isDirectExpectCall?: boolean,
},
) => {
const secondArgument = options && options.secondArgument;
const isDirectExpectCall = options && options.isDirectExpectCall;
return (
chalk.dim('expect' + (isDirectExpectCall ? '' : '(')) +
RECEIVED_COLOR(received) +
chalk.dim((isDirectExpectCall ? '' : ')') + matcherName + '(') +
EXPECTED_COLOR(expected) +
(secondArgument ? `, ${EXPECTED_COLOR(secondArgument)}` : '') +
chalk.dim(')')
);
};
module.exports = {
EXPECTED_BG,
EXPECTED_COLOR,
RECEIVED_BG,
RECEIVED_COLOR,
SUGGEST_TO_EQUAL,
ensureActualIsNumber,
ensureExpectedIsNumber,
ensureNoExpected,
ensureNumbers,
highlightTrailingWhitespace,
matcherHint,
pluralize,
printExpected,
printReceived,
printWithType,
stringify,
};