@@ -32,6 +32,25 @@ export function serializeValue(val: any, seen: WeakMap<WeakKey, any> = new WeakM
32
32
if ( ! val || typeof val === 'string' ) {
33
33
return val
34
34
}
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
+ }
35
54
if ( typeof val === 'function' ) {
36
55
return `Function<${ val . name || 'anonymous' } >`
37
56
}
@@ -106,6 +125,15 @@ export function serializeValue(val: any, seen: WeakMap<WeakKey, any> = new WeakM
106
125
}
107
126
}
108
127
128
+ function safe ( fn : ( ) => void ) {
129
+ try {
130
+ return fn ( )
131
+ }
132
+ catch {
133
+ // ignore
134
+ }
135
+ }
136
+
109
137
export { serializeValue as serializeError }
110
138
111
139
function normalizeErrorMessage ( message : string ) {
@@ -122,15 +150,6 @@ export function processError(
122
150
}
123
151
const err = _err as TestError
124
152
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
-
134
153
if (
135
154
err . showDiff
136
155
|| ( err . showDiff === undefined
@@ -143,10 +162,10 @@ export function processError(
143
162
} )
144
163
}
145
164
146
- if ( typeof err . expected !== 'string' ) {
165
+ if ( 'expected' in err && typeof err . expected !== 'string' ) {
147
166
err . expected = stringify ( err . expected , 10 )
148
167
}
149
- if ( typeof err . actual !== 'string' ) {
168
+ if ( 'actual' in err && typeof err . actual !== 'string' ) {
150
169
err . actual = stringify ( err . actual , 10 )
151
170
}
152
171
0 commit comments