@@ -32,6 +32,25 @@ export function serializeValue(val: any, seen: WeakMap<WeakKey, any> = new WeakM
3232 if ( ! val || typeof val === 'string' ) {
3333 return val
3434 }
35+ if ( val instanceof Error && 'toJSON' in val && typeof val . toJSON === 'function' ) {
36+ const jsonValue = val . toJSON ( )
37+
38+ if ( jsonValue && jsonValue !== val && typeof jsonValue === 'object' ) {
39+ if ( typeof val . message === 'string' ) {
40+ safe ( ( ) => jsonValue . message ??= val . message )
41+ }
42+ if ( typeof val . stack === 'string' ) {
43+ safe ( ( ) => jsonValue . stack ??= val . stack )
44+ }
45+ if ( typeof val . name === 'string' ) {
46+ safe ( ( ) => jsonValue . name ??= val . name )
47+ }
48+ if ( val . cause != null ) {
49+ safe ( ( ) => jsonValue . cause ??= serializeValue ( val . cause , seen ) )
50+ }
51+ }
52+ return serializeValue ( jsonValue , seen )
53+ }
3554 if ( typeof val === 'function' ) {
3655 return `Function<${ val . name || 'anonymous' } >`
3756 }
@@ -106,6 +125,15 @@ export function serializeValue(val: any, seen: WeakMap<WeakKey, any> = new WeakM
106125 }
107126}
108127
128+ function safe ( fn : ( ) => void ) {
129+ try {
130+ return fn ( )
131+ }
132+ catch {
133+ // ignore
134+ }
135+ }
136+
109137export { serializeValue as serializeError }
110138
111139function normalizeErrorMessage ( message : string ) {
@@ -122,15 +150,6 @@ export function processError(
122150 }
123151 const err = _err as TestError
124152
125- // stack is not serialized in worker communication
126- // we stringify it first
127- if ( typeof err . stack === 'string' ) {
128- err . stackStr = String ( err . stack )
129- }
130- if ( typeof err . name === 'string' ) {
131- err . nameStr = String ( err . name )
132- }
133-
134153 if (
135154 err . showDiff
136155 || ( err . showDiff === undefined
@@ -143,10 +162,10 @@ export function processError(
143162 } )
144163 }
145164
146- if ( typeof err . expected !== 'string' ) {
165+ if ( 'expected' in err && typeof err . expected !== 'string' ) {
147166 err . expected = stringify ( err . expected , 10 )
148167 }
149- if ( typeof err . actual !== 'string' ) {
168+ if ( 'actual' in err && typeof err . actual !== 'string' ) {
150169 err . actual = stringify ( err . actual , 10 )
151170 }
152171
0 commit comments