Compare error name and message on Bun.deepEquals and assert.deepStrictEqual (#7867)

* check error name and message on Bun.deepEquals

* add tests

* add test with subclass of Error
This commit is contained in:
Kaio Duarte
2023-12-29 00:32:49 +00:00
committed by GitHub
parent eac3bda895
commit 14bc121dc8
2 changed files with 26 additions and 0 deletions

View File

@@ -830,6 +830,24 @@ bool Bun__deepEquals(JSC__JSGlobalObject* globalObject, JSValue v1, JSValue v2,
return false;
}
case ErrorInstanceType: {
if (c2Type != ErrorInstanceType) {
return false;
}
if (JSC::ErrorInstance* left = jsDynamicCast<JSC::ErrorInstance*>(v1)) {
JSC::ErrorInstance* right = jsDynamicCast<JSC::ErrorInstance*>(v2);
if (UNLIKELY(!right)) {
return false;
}
return (
left->sanitizedNameString(globalObject) == right->sanitizedNameString(globalObject) &&
left->sanitizedMessageString(globalObject) == right->sanitizedMessageString(globalObject)
);
}
}
case Int8ArrayType:
case Uint8ArrayType:
case Uint8ClampedArrayType:

View File

@@ -161,6 +161,14 @@ test("testing Bun.deepEquals() using isEqual()", () => {
expect(Infinity).toEqual(1 / 0);
expect(-Infinity).toEqual(-Infinity);
expect(-Infinity).toEqual(-1 / 0);
expect(Error("foo")).toEqual(Error("foo"));
expect(Error("foo")).not.toEqual(Error("bar"));
expect(Error("foo")).not.toEqual("foo");
class CustomError extends Error { constructor(message) { super(message); } };
expect(new CustomError("foo")).not.toEqual(new CustomError("bar"));
expect(new CustomError("foo")).toEqual(new CustomError("foo"));
});
try {