Skip to content

Commit 887c2b6

Browse files
committed
WIP Better handling of causes
1 parent 48ddbf4 commit 887c2b6

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

packages/jest-message-util/src/index.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ export const formatStackTrace = (
327327
};
328328

329329
type FailedResults = Array<{
330-
content: string;
330+
error: unknown;
331331
result: TestResult.AssertionResult;
332332
}>;
333333

@@ -339,8 +339,8 @@ export const formatResultsErrors = (
339339
): string | null => {
340340
const failedResults: FailedResults = testResults.reduce<FailedResults>(
341341
(errors, result) => {
342-
result.failureMessages.forEach(item => {
343-
errors.push({content: checkForCommonEnvironmentErrors(item), result});
342+
result.failureDetails.forEach(item => {
343+
errors.push({error: item, result});
344344
});
345345
return errors;
346346
},
@@ -352,15 +352,22 @@ export const formatResultsErrors = (
352352
}
353353

354354
return failedResults
355-
.map(({result, content}) => {
356-
let {message, stack} = separateMessageFromStack(content);
357-
stack = options.noStackTrace
358-
? ''
359-
: `${STACK_TRACE_COLOR(
360-
formatStackTrace(stack, config, options, testPath),
361-
)}\n`;
362-
363-
message = indentAllLines(message);
355+
.map(({result, error}) => {
356+
function formatOne(error: Error, noIndent: boolean): string {
357+
let {message, stack, cause} = error;
358+
stack = options.noStackTrace
359+
? ''
360+
: `${STACK_TRACE_COLOR(
361+
formatStackTrace(stack || '', config, options, testPath),
362+
)}\n`;
363+
364+
const newMessage = noIndent ? message : indentAllLines(message);
365+
366+
if (cause !== undefined) {
367+
return `${newMessage}\n${stack}\nCause: ${formatOne(cause, true)}`;
368+
}
369+
return `${newMessage}\n${stack}`;
370+
}
364371

365372
const title = `${chalk.bold.red(
366373
TITLE_INDENT +
@@ -370,7 +377,7 @@ export const formatResultsErrors = (
370377
result.title,
371378
)}\n`;
372379

373-
return `${title}\n${message}\n${stack}`;
380+
return `${title}\n${formatOne(error as Error, false)}`;
374381
})
375382
.join('\n');
376383
};

0 commit comments

Comments
 (0)