MCPcopy
hub / github.com/jestjs/jest / assertionErrorMessage

Function assertionErrorMessage

packages/jest-circus/src/formatNodeAssertErrors.ts:133–194  ·  view source on GitHub ↗
(
  error: AssertionErrorWithStack,
  options: DiffOptions,
)

Source from the content-addressed store, hash-verified

131};
132
133function assertionErrorMessage(
134 error: AssertionErrorWithStack,
135 options: DiffOptions,
136) {
137 const {expected, actual, generatedMessage, message, operator, stack} = error;
138 const diffString = diff(expected, actual, options);
139 const hasCustomMessage = !generatedMessage;
140 const operatorName = getOperatorName(operator, stack);
141 const trimmedStack = stack
142 .replace(message, '')
143 .replaceAll(/AssertionError(.*)/g, '');
144
145 if (operatorName === 'doesNotThrow') {
146 return (
147 // eslint-disable-next-line prefer-template
148 buildHintString(assertThrowingMatcherHint(operatorName)) +
149 chalk.reset('Expected the function not to throw an error.\n') +
150 chalk.reset('Instead, it threw:\n') +
151 ` ${printReceived(actual)}` +
152 chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '') +
153 trimmedStack
154 );
155 }
156
157 if (operatorName === 'throws') {
158 if (error.generatedMessage) {
159 return (
160 buildHintString(assertThrowingMatcherHint(operatorName)) +
161 chalk.reset(error.message) +
162 chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '') +
163 trimmedStack
164 );
165 }
166 return (
167 buildHintString(assertThrowingMatcherHint(operatorName)) +
168 chalk.reset('Expected the function to throw an error.\n') +
169 chalk.reset("But it didn't throw anything.") +
170 chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '') +
171 trimmedStack
172 );
173 }
174
175 if (operatorName === 'fail') {
176 return (
177 buildHintString(assertMatcherHint(operator, operatorName, expected)) +
178 chalk.reset(hasCustomMessage ? `Message:\n ${message}` : '') +
179 trimmedStack
180 );
181 }
182
183 return (
184 // eslint-disable-next-line prefer-template
185 buildHintString(assertMatcherHint(operator, operatorName, expected)) +
186 chalk.reset(`Expected value ${operatorMessage(operator)}`) +
187 ` ${printExpected(expected)}\n` +
188 chalk.reset('Received:\n') +
189 ` ${printReceived(actual)}` +
190 chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '') +

Callers 1

formatNodeAssertErrorsFunction · 0.70

Calls 9

diffFunction · 0.90
printReceivedFunction · 0.90
printExpectedFunction · 0.90
getOperatorNameFunction · 0.70
buildHintStringFunction · 0.70
assertMatcherHintFunction · 0.70
operatorMessageFunction · 0.70
resetMethod · 0.45

Tested by

no test coverage detected