@@ -122,18 +122,20 @@ function warnAboutWrongTestEnvironment(error: string, env: 'jsdom' | 'node') {
122122// `before/after each` hooks). If it's thrown, none of the tests in the file
123123// are executed.
124124export const formatExecError = (
125- error : Error | TestResult . SerializableError | string | undefined ,
125+ error : Error | TestResult . SerializableError | string | undefined | number ,
126126 config : StackTraceConfig ,
127127 options : StackTraceOptions ,
128128 testPath ?: string ,
129129 reuseMessage ?: boolean ,
130+ noTitle ?: boolean ,
130131) : string => {
131132 if ( ! error || typeof error === 'number' ) {
132133 error = new Error ( `Expected an Error, but "${ String ( error ) } " was thrown` ) ;
133134 error . stack = '' ;
134135 }
135136
136137 let message , stack ;
138+ let cause = '' ;
137139
138140 if ( typeof error === 'string' || ! error ) {
139141 error || ( error = 'EMPTY ERROR' ) ;
@@ -145,6 +147,27 @@ export const formatExecError = (
145147 typeof error . stack === 'string'
146148 ? error . stack
147149 : `thrown: ${ prettyFormat ( error , { maxDepth : 3 } ) } ` ;
150+ if ( 'cause' in error ) {
151+ const prefix = '\n\nCause:\n' ;
152+ if ( typeof error . cause === 'string' ) {
153+ cause += `${ prefix } ${ error . cause } ` ;
154+ } else if ( typeof error . cause === 'number' ) {
155+ cause += `${ prefix } ${ error . cause . toString ( ) } ` ;
156+ } else if ( error . cause instanceof Error ) {
157+ const formatted = formatExecError (
158+ error . cause ,
159+ config ,
160+ options ,
161+ testPath ,
162+ reuseMessage ,
163+ true ,
164+ ) ;
165+ cause += `${ prefix } ${ formatted } ` ;
166+ }
167+ }
168+ }
169+ if ( cause !== '' ) {
170+ cause = indentAllLines ( cause ) ;
148171 }
149172
150173 const separated = separateMessageFromStack ( stack || '' ) ;
@@ -174,13 +197,14 @@ export const formatExecError = (
174197
175198 let messageToUse ;
176199
177- if ( reuseMessage ) {
200+ if ( reuseMessage || noTitle ) {
178201 messageToUse = ` ${ message . trim ( ) } ` ;
179202 } else {
180203 messageToUse = `${ EXEC_ERROR_MESSAGE } \n\n${ message } ` ;
181204 }
205+ const title = noTitle ? '' : `${ TITLE_INDENT + TITLE_BULLET } ` ;
182206
183- return `${ TITLE_INDENT + TITLE_BULLET + messageToUse + stack } \n` ;
207+ return `${ title + messageToUse + stack + cause } \n` ;
184208} ;
185209
186210const removeInternalStackEntries = (
0 commit comments