-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
formatNodeAssertErrors.ts
189 lines (167 loc) · 5.1 KB
/
formatNodeAssertErrors.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
/**
* 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 {AssertionError} from 'assert';
import {Circus} from '@jest/types';
import {
diff,
printExpected,
printReceived,
DiffOptions,
} from 'jest-matcher-utils';
import chalk from 'chalk';
import prettyFormat from 'pretty-format';
interface AssertionErrorWithStack extends AssertionError {
stack: string;
}
const assertOperatorsMap: {[key: string]: string} = {
'!=': 'notEqual',
'!==': 'notStrictEqual',
'==': 'equal',
'===': 'strictEqual',
};
const humanReadableOperators: {[key: string]: string} = {
deepEqual: 'to deeply equal',
deepStrictEqual: 'to deeply and strictly equal',
equal: 'to be equal',
notDeepEqual: 'not to deeply equal',
notDeepStrictEqual: 'not to deeply and strictly equal',
notEqual: 'to not be equal',
notStrictEqual: 'not be strictly equal',
strictEqual: 'to strictly be equal',
};
const formatNodeAssertErrors = (event: Circus.Event, state: Circus.State) => {
switch (event.name) {
case 'test_done': {
event.test.errors = event.test.errors.map((errors: Circus.TestError) => {
let error;
if (Array.isArray(errors)) {
const [originalError, asyncError] = errors;
if (originalError == null) {
error = asyncError;
} else if (!originalError.stack) {
error = asyncError;
error.message = originalError.message
? originalError.message
: `thrown: ${prettyFormat(originalError, {maxDepth: 3})}`;
} else {
error = originalError;
}
} else {
error = errors;
}
return isAssertionError(error)
? {message: assertionErrorMessage(error, {expand: state.expand})}
: errors;
});
}
}
};
const getOperatorName = (operator: string | undefined, stack: string) => {
if (typeof operator === 'string') {
return assertOperatorsMap[operator] || operator;
}
if (stack.match('.doesNotThrow')) {
return 'doesNotThrow';
}
if (stack.match('.throws')) {
return 'throws';
}
return '';
};
const operatorMessage = (operator: string | undefined) => {
const niceOperatorName = getOperatorName(operator, '');
const humanReadableOperator = humanReadableOperators[niceOperatorName];
return typeof operator === 'string'
? `${humanReadableOperator || niceOperatorName} to:\n`
: '';
};
const assertThrowingMatcherHint = (operatorName: string) =>
operatorName
? chalk.dim('assert') +
chalk.dim('.' + operatorName + '(') +
chalk.red('function') +
chalk.dim(')')
: '';
const assertMatcherHint = (
operator: string | undefined | null,
operatorName: string,
) => {
let message = '';
if (operatorName) {
message =
chalk.dim('assert') +
chalk.dim('.' + operatorName + '(') +
chalk.red('received') +
chalk.dim(', ') +
chalk.green('expected') +
chalk.dim(')');
}
if (operator === '==') {
message +=
' or ' +
chalk.dim('assert') +
chalk.dim('(') +
chalk.red('received') +
chalk.dim(') ');
}
return message;
};
function assertionErrorMessage(
error: AssertionErrorWithStack,
options: DiffOptions,
) {
const {expected, actual, generatedMessage, message, operator, stack} = error;
const diffString = diff(expected, actual, options);
const hasCustomMessage = !generatedMessage;
const operatorName = getOperatorName(operator, stack);
const trimmedStack = stack
.replace(message, '')
.replace(/AssertionError(.*)/g, '');
if (operatorName === 'doesNotThrow') {
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
chalk.reset(`Expected the function not to throw an error.\n`) +
chalk.reset(`Instead, it threw:\n`) +
` ${printReceived(actual)}` +
chalk.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') +
trimmedStack
);
}
if (operatorName === 'throws') {
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
chalk.reset(`Expected the function to throw an error.\n`) +
chalk.reset(`But it didn't throw anything.`) +
chalk.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') +
trimmedStack
);
}
return (
buildHintString(assertMatcherHint(operator, operatorName)) +
chalk.reset(`Expected value ${operatorMessage(operator)}`) +
` ${printExpected(expected)}\n` +
chalk.reset(`Received:\n`) +
` ${printReceived(actual)}` +
chalk.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') +
(diffString ? `\n\nDifference:\n\n${diffString}` : '') +
trimmedStack
);
}
function isAssertionError(
error: Circus.TestError,
): error is AssertionErrorWithStack {
return (
error &&
(error instanceof AssertionError ||
error.name === AssertionError.name ||
error.code === 'ERR_ASSERTION')
);
}
function buildHintString(hint: string) {
return hint ? hint + '\n\n' : '';
}
export default formatNodeAssertErrors;