1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _chalk = _interopRequireDefault(require('chalk'));
|
9 |
|
10 | var _jestMatcherUtils = require('jest-matcher-utils');
|
11 |
|
12 | function _interopRequireDefault(obj) {
|
13 | return obj && obj.__esModule ? obj : {default: obj};
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const assertOperatorsMap = {
|
23 | '!=': 'notEqual',
|
24 | '!==': 'notStrictEqual',
|
25 | '==': 'equal',
|
26 | '===': 'strictEqual'
|
27 | };
|
28 | const humanReadableOperators = {
|
29 | deepEqual: 'to deeply equal',
|
30 | deepStrictEqual: 'to deeply and strictly equal',
|
31 | equal: 'to be equal',
|
32 | notDeepEqual: 'not to deeply equal',
|
33 | notDeepStrictEqual: 'not to deeply and strictly equal',
|
34 | notEqual: 'to not be equal',
|
35 | notStrictEqual: 'not be strictly equal',
|
36 | strictEqual: 'to strictly be equal'
|
37 | };
|
38 |
|
39 | const getOperatorName = (operator, stack) => {
|
40 | if (typeof operator === 'string') {
|
41 | return assertOperatorsMap[operator] || operator;
|
42 | }
|
43 |
|
44 | if (stack.match('.doesNotThrow')) {
|
45 | return 'doesNotThrow';
|
46 | }
|
47 |
|
48 | if (stack.match('.throws')) {
|
49 | return 'throws';
|
50 | }
|
51 |
|
52 | if (stack.match('.fail')) {
|
53 | return 'fail';
|
54 | }
|
55 |
|
56 | return '';
|
57 | };
|
58 |
|
59 | const operatorMessage = operator => {
|
60 | const niceOperatorName = getOperatorName(operator, '');
|
61 | const humanReadableOperator = humanReadableOperators[niceOperatorName];
|
62 | return typeof operator === 'string'
|
63 | ? `${humanReadableOperator || niceOperatorName} to:\n`
|
64 | : '';
|
65 | };
|
66 |
|
67 | const assertThrowingMatcherHint = operatorName =>
|
68 | operatorName
|
69 | ? _chalk.default.dim('assert') +
|
70 | _chalk.default.dim('.' + operatorName + '(') +
|
71 | _chalk.default.red('function') +
|
72 | _chalk.default.dim(')')
|
73 | : '';
|
74 |
|
75 | const assertMatcherHint = (operator, operatorName, expected) => {
|
76 | let message = '';
|
77 |
|
78 | if (operator === '==' && expected === true) {
|
79 | message =
|
80 | _chalk.default.dim('assert') +
|
81 | _chalk.default.dim('(') +
|
82 | _chalk.default.red('received') +
|
83 | _chalk.default.dim(')');
|
84 | } else if (operatorName) {
|
85 | message =
|
86 | _chalk.default.dim('assert') +
|
87 | _chalk.default.dim('.' + operatorName + '(') +
|
88 | _chalk.default.red('received') +
|
89 | _chalk.default.dim(', ') +
|
90 | _chalk.default.green('expected') +
|
91 | _chalk.default.dim(')');
|
92 | }
|
93 |
|
94 | return message;
|
95 | };
|
96 |
|
97 | function assertionErrorMessage(error, options) {
|
98 | const {expected, actual, generatedMessage, message, operator, stack} = error;
|
99 | const diffString = (0, _jestMatcherUtils.diff)(expected, actual, options);
|
100 | const hasCustomMessage = !generatedMessage;
|
101 | const operatorName = getOperatorName(operator, stack);
|
102 | const trimmedStack = stack
|
103 | .replace(message, '')
|
104 | .replace(/AssertionError(.*)/g, '');
|
105 |
|
106 | if (operatorName === 'doesNotThrow') {
|
107 | return (
|
108 | buildHintString(assertThrowingMatcherHint(operatorName)) +
|
109 | _chalk.default.reset(`Expected the function not to throw an error.\n`) +
|
110 | _chalk.default.reset(`Instead, it threw:\n`) +
|
111 | ` ${(0, _jestMatcherUtils.printReceived)(actual)}` +
|
112 | _chalk.default.reset(
|
113 | hasCustomMessage ? '\n\nMessage:\n ' + message : ''
|
114 | ) +
|
115 | trimmedStack
|
116 | );
|
117 | }
|
118 |
|
119 | if (operatorName === 'throws') {
|
120 | return (
|
121 | buildHintString(assertThrowingMatcherHint(operatorName)) +
|
122 | _chalk.default.reset(`Expected the function to throw an error.\n`) +
|
123 | _chalk.default.reset(`But it didn't throw anything.`) +
|
124 | _chalk.default.reset(
|
125 | hasCustomMessage ? '\n\nMessage:\n ' + message : ''
|
126 | ) +
|
127 | trimmedStack
|
128 | );
|
129 | }
|
130 |
|
131 | if (operatorName === 'fail') {
|
132 | return (
|
133 | buildHintString(assertMatcherHint(operator, operatorName, expected)) +
|
134 | _chalk.default.reset(hasCustomMessage ? 'Message:\n ' + message : '') +
|
135 | trimmedStack
|
136 | );
|
137 | }
|
138 |
|
139 | return (
|
140 | buildHintString(assertMatcherHint(operator, operatorName, expected)) +
|
141 | _chalk.default.reset(`Expected value ${operatorMessage(operator)}`) +
|
142 | ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
|
143 | _chalk.default.reset(`Received:\n`) +
|
144 | ` ${(0, _jestMatcherUtils.printReceived)(actual)}` +
|
145 | _chalk.default.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') +
|
146 | (diffString ? `\n\nDifference:\n\n${diffString}` : '') +
|
147 | trimmedStack
|
148 | );
|
149 | }
|
150 |
|
151 | function buildHintString(hint) {
|
152 | return hint ? hint + '\n\n' : '';
|
153 | }
|
154 |
|
155 | var _default = assertionErrorMessage;
|
156 | exports.default = _default;
|