7
7
8
8
import * as path from 'path' ;
9
9
import { fileURLToPath } from 'url' ;
10
+ import { types } from 'util' ;
10
11
import { codeFrameColumns } from '@babel/code-frame' ;
11
12
import chalk = require( 'chalk' ) ;
12
13
import * as fs from 'graceful-fs' ;
@@ -122,18 +123,20 @@ function warnAboutWrongTestEnvironment(error: string, env: 'jsdom' | 'node') {
122
123
// `before/after each` hooks). If it's thrown, none of the tests in the file
123
124
// are executed.
124
125
export const formatExecError = (
125
- error : Error | TestResult . SerializableError | string | undefined ,
126
+ error : Error | TestResult . SerializableError | string | number | undefined ,
126
127
config : StackTraceConfig ,
127
128
options : StackTraceOptions ,
128
129
testPath ?: string ,
129
130
reuseMessage ?: boolean ,
131
+ noTitle ?: boolean ,
130
132
) : string => {
131
133
if ( ! error || typeof error === 'number' ) {
132
134
error = new Error ( `Expected an Error, but "${ String ( error ) } " was thrown` ) ;
133
135
error . stack = '' ;
134
136
}
135
137
136
138
let message , stack ;
139
+ let cause = '' ;
137
140
138
141
if ( typeof error === 'string' || ! error ) {
139
142
error || ( error = 'EMPTY ERROR' ) ;
@@ -145,6 +148,25 @@ export const formatExecError = (
145
148
typeof error . stack === 'string'
146
149
? error . stack
147
150
: `thrown: ${ prettyFormat ( error , { maxDepth : 3 } ) } ` ;
151
+ if ( 'cause' in error ) {
152
+ const prefix = '\n\nCause:\n' ;
153
+ if ( typeof error . cause === 'string' || typeof error . cause === 'number' ) {
154
+ cause += `${ prefix } ${ error . cause } ` ;
155
+ } else if ( types . isNativeError ( error . cause ) ) {
156
+ const formatted = formatExecError (
157
+ error . cause ,
158
+ config ,
159
+ options ,
160
+ testPath ,
161
+ reuseMessage ,
162
+ true ,
163
+ ) ;
164
+ cause += `${ prefix } ${ formatted } ` ;
165
+ }
166
+ }
167
+ }
168
+ if ( cause !== '' ) {
169
+ cause = indentAllLines ( cause ) ;
148
170
}
149
171
150
172
const separated = separateMessageFromStack ( stack || '' ) ;
@@ -174,13 +196,14 @@ export const formatExecError = (
174
196
175
197
let messageToUse ;
176
198
177
- if ( reuseMessage ) {
199
+ if ( reuseMessage || noTitle ) {
178
200
messageToUse = ` ${ message . trim ( ) } ` ;
179
201
} else {
180
202
messageToUse = `${ EXEC_ERROR_MESSAGE } \n\n${ message } ` ;
181
203
}
204
+ const title = noTitle ? '' : `${ TITLE_INDENT + TITLE_BULLET } ` ;
182
205
183
- return `${ TITLE_INDENT + TITLE_BULLET + messageToUse + stack } \n` ;
206
+ return `${ title + messageToUse + stack + cause } \n` ;
184
207
} ;
185
208
186
209
const removeInternalStackEntries = (
0 commit comments