Skip to content

Commit 714d34e

Browse files
committed
fix(diff): truncate to avoid crash on diff large objects
1 parent b700d26 commit 714d34e

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

packages/utils/src/diff/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const FORMAT_OPTIONS = {
5757
}
5858
const FALLBACK_FORMAT_OPTIONS = {
5959
callToJSON: false,
60-
maxDepth: 10,
60+
maxDepth: 8,
6161
plugins: PLUGINS,
6262
}
6363

@@ -97,8 +97,18 @@ export function diff(a: any, b: any, options?: DiffOptions): string | undefined
9797
const { aAnnotation, aColor, aIndicator, bAnnotation, bColor, bIndicator }
9898
= normalizeDiffOptions(options)
9999
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options)
100-
const aDisplay = prettyFormat(a, formatOptions)
101-
const bDisplay = prettyFormat(b, formatOptions)
100+
let aDisplay = prettyFormat(a, formatOptions)
101+
let bDisplay = prettyFormat(b, formatOptions)
102+
// even if prettyFormat prints successfully big objects,
103+
// large string can choke later on (concatenation? RPC?),
104+
// so truncate it to a reasonable length here.
105+
// (For example, playwright's ElementHandle can become about 200_000_000 length string)
106+
const MAX_LENGTH = 100_000
107+
function truncate(s: string) {
108+
return s.length <= MAX_LENGTH ? s : (`${s.slice(0, MAX_LENGTH)}...`)
109+
}
110+
aDisplay = truncate(aDisplay)
111+
bDisplay = truncate(bDisplay)
102112
const aDiff = `${aColor(`${aIndicator} ${aAnnotation}:`)} \n${aDisplay}`
103113
const bDiff = `${bColor(`${bIndicator} ${bAnnotation}:`)} \n${bDisplay}`
104114
return `${aDiff}\n\n${bDiff}`

0 commit comments

Comments
 (0)