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

Function assertionErrorMessage

packages/jest-jasmine2/src/assertionErrorMessage.ts:91–147  ·  view source on GitHub ↗
(
  error: AssertionErrorWithStack,
  options: DiffOptions,
)

Source from the content-addressed store, hash-verified

89};
90
91function assertionErrorMessage(
92 error: AssertionErrorWithStack,
93 options: DiffOptions,
94): string {
95 const {expected, actual, generatedMessage, message, operator, stack} = error;
96 const diffString = diff(expected, actual, options);
97 const hasCustomMessage = !generatedMessage;
98 const operatorName = getOperatorName(operator, stack);
99 const trimmedStack = stack
100 .replace(message, '')
101 .replaceAll(/AssertionError(.*)/g, '');
102
103 if (operatorName === 'doesNotThrow') {
104 return `${
105 buildHintString(assertThrowingMatcherHint(operatorName)) +
106 chalk.reset('Expected the function not to throw an error.\n') +
107 chalk.reset('Instead, it threw:\n')
108 } ${printReceived(actual)}${chalk.reset(
109 hasCustomMessage ? `\n\nMessage:\n ${message}` : '',
110 )}${trimmedStack}`;
111 }
112
113 if (operatorName === 'throws') {
114 if (error.generatedMessage) {
115 return (
116 buildHintString(assertThrowingMatcherHint(operatorName)) +
117 chalk.reset(error.message) +
118 chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '') +
119 trimmedStack
120 );
121 }
122 return (
123 buildHintString(assertThrowingMatcherHint(operatorName)) +
124 chalk.reset('Expected the function to throw an error.\n') +
125 chalk.reset("But it didn't throw anything.") +
126 chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '') +
127 trimmedStack
128 );
129 }
130
131 if (operatorName === 'fail') {
132 return (
133 buildHintString(assertMatcherHint(operator, operatorName, expected)) +
134 chalk.reset(hasCustomMessage ? `Message:\n ${message}` : '') +
135 trimmedStack
136 );
137 }
138
139 return `${
140 buildHintString(assertMatcherHint(operator, operatorName, expected)) +
141 chalk.reset(`Expected value ${operatorMessage(operator)}`)
142 } ${printExpected(expected)}\n${chalk.reset('Received:\n')} ${printReceived(
143 actual,
144 )}${chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '')}${
145 diffString ? `\n\nDifference:\n\n${diffString}` : ''
146 }${trimmedStack}`;
147}
148

Callers 2

constructorMethod · 0.50
onExceptionMethod · 0.50

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