assertionErrorMessage.js
4.49 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
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = void 0;
var _chalk = _interopRequireDefault(require('chalk'));
var _jestMatcherUtils = require('jest-matcher-utils');
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
/**
* 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.
*/
const assertOperatorsMap = {
'!=': 'notEqual',
'!==': 'notStrictEqual',
'==': 'equal',
'===': 'strictEqual'
};
const humanReadableOperators = {
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 getOperatorName = (operator, stack) => {
if (typeof operator === 'string') {
return assertOperatorsMap[operator] || operator;
}
if (stack.match('.doesNotThrow')) {
return 'doesNotThrow';
}
if (stack.match('.throws')) {
return 'throws';
} // this fallback is only needed for versions older than node 10
if (stack.match('.fail')) {
return 'fail';
}
return '';
};
const operatorMessage = operator => {
const niceOperatorName = getOperatorName(operator, '');
const humanReadableOperator = humanReadableOperators[niceOperatorName];
return typeof operator === 'string'
? `${humanReadableOperator || niceOperatorName} to:\n`
: '';
};
const assertThrowingMatcherHint = operatorName =>
operatorName
? _chalk.default.dim('assert') +
_chalk.default.dim('.' + operatorName + '(') +
_chalk.default.red('function') +
_chalk.default.dim(')')
: '';
const assertMatcherHint = (operator, operatorName, expected) => {
let message = '';
if (operator === '==' && expected === true) {
message =
_chalk.default.dim('assert') +
_chalk.default.dim('(') +
_chalk.default.red('received') +
_chalk.default.dim(')');
} else if (operatorName) {
message =
_chalk.default.dim('assert') +
_chalk.default.dim('.' + operatorName + '(') +
_chalk.default.red('received') +
_chalk.default.dim(', ') +
_chalk.default.green('expected') +
_chalk.default.dim(')');
}
return message;
};
function assertionErrorMessage(error, options) {
const {expected, actual, generatedMessage, message, operator, stack} = error;
const diffString = (0, _jestMatcherUtils.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.default.reset('Expected the function not to throw an error.\n') +
_chalk.default.reset('Instead, it threw:\n') +
` ${(0, _jestMatcherUtils.printReceived)(actual)}` +
_chalk.default.reset(
hasCustomMessage ? '\n\nMessage:\n ' + message : ''
) +
trimmedStack
);
}
if (operatorName === 'throws') {
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
_chalk.default.reset('Expected the function to throw an error.\n') +
_chalk.default.reset("But it didn't throw anything.") +
_chalk.default.reset(
hasCustomMessage ? '\n\nMessage:\n ' + message : ''
) +
trimmedStack
);
}
if (operatorName === 'fail') {
return (
buildHintString(assertMatcherHint(operator, operatorName, expected)) +
_chalk.default.reset(hasCustomMessage ? 'Message:\n ' + message : '') +
trimmedStack
);
}
return (
buildHintString(assertMatcherHint(operator, operatorName, expected)) +
_chalk.default.reset(`Expected value ${operatorMessage(operator)}`) +
` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
_chalk.default.reset('Received:\n') +
` ${(0, _jestMatcherUtils.printReceived)(actual)}` +
_chalk.default.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') +
(diffString ? `\n\nDifference:\n\n${diffString}` : '') +
trimmedStack
);
}
function buildHintString(hint) {
return hint ? hint + '\n\n' : '';
}
var _default = assertionErrorMessage;
exports.default = _default;