Skip to content

Commit

Permalink
WIP Better handling of causes
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Feb 18, 2023
1 parent 48ddbf4 commit 887c2b6
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions packages/jest-message-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export const formatStackTrace = (
};

type FailedResults = Array<{
content: string;
error: unknown;
result: TestResult.AssertionResult;
}>;

Expand All @@ -339,8 +339,8 @@ export const formatResultsErrors = (
): string | null => {
const failedResults: FailedResults = testResults.reduce<FailedResults>(
(errors, result) => {
result.failureMessages.forEach(item => {
errors.push({content: checkForCommonEnvironmentErrors(item), result});
result.failureDetails.forEach(item => {
errors.push({error: item, result});
});
return errors;
},
Expand All @@ -352,15 +352,22 @@ export const formatResultsErrors = (
}

return failedResults
.map(({result, content}) => {
let {message, stack} = separateMessageFromStack(content);
stack = options.noStackTrace
? ''
: `${STACK_TRACE_COLOR(
formatStackTrace(stack, config, options, testPath),
)}\n`;

message = indentAllLines(message);
.map(({result, error}) => {
function formatOne(error: Error, noIndent: boolean): string {
let {message, stack, cause} = error;
stack = options.noStackTrace
? ''
: `${STACK_TRACE_COLOR(
formatStackTrace(stack || '', config, options, testPath),
)}\n`;

const newMessage = noIndent ? message : indentAllLines(message);

if (cause !== undefined) {
return `${newMessage}\n${stack}\nCause: ${formatOne(cause, true)}`;
}
return `${newMessage}\n${stack}`;
}

const title = `${chalk.bold.red(
TITLE_INDENT +
Expand All @@ -370,7 +377,7 @@ export const formatResultsErrors = (
result.title,
)}\n`;

return `${title}\n${message}\n${stack}`;
return `${title}\n${formatOne(error as Error, false)}`;
})
.join('\n');
};
Expand Down

0 comments on commit 887c2b6

Please sign in to comment.