use bun's equal check

This commit is contained in:
Chris Wanstrath 2025-10-29 12:50:09 -07:00
parent c3453fdc5c
commit e95c0d6728

View File

@ -108,16 +108,10 @@ expect.extend({
if (expected instanceof RegExp) expected = String(expected) if (expected instanceof RegExp) expected = String(expected)
if (value instanceof RegExp) value = String(value) if (value instanceof RegExp) value = String(value)
if (isEqual(value, expected)) { expect(value).toEqual(expected)
return { pass: true }
} else {
return { return {
message: () => message: () => `Expected evaluation to be ${expected}, but got ${value}`,
`Expected evaluation to be ${JSON.stringify(expected)}, but got ${JSON.stringify( pass: true,
value
)}`,
pass: false,
}
} }
} catch (error) { } catch (error) {
return { return {
@ -167,29 +161,3 @@ const trimWhitespace = (str: string): string => {
}) })
.join('\n') .join('\n')
} }
function isEqual(a: any, b: any): boolean {
if (a === null && b === null) return true
switch (typeof a) {
case 'string':
case 'number':
case 'boolean':
case 'undefined':
return a === b
default:
return JSON.stringify(sortKeys(a)) === JSON.stringify(sortKeys(b))
}
}
function sortKeys(o: any): any {
if (Array.isArray(o)) return o.map(sortKeys)
if (o && typeof o === 'object' && o.constructor === Object)
return Object.keys(o)
.sort()
.reduce((r, k) => {
r[k] = sortKeys(o[k])
return r
}, {} as any)
return o
}