Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/jsc/modules/NodeUtilTypesModule.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "BunClientData.h"
#include "JSDOMException.h"
#include "JSDOMURL.h"
#include "JSDOMWrapper.h"
#include "node/crypto/JSKeyObject.h"
Expand Down Expand Up @@ -559,7 +560,7 @@ static bool setSubset(JSC::JSGlobalObject* globalObject, JSC::MarkedArgumentBuff
return true;
}

// Errors compare name/message/errors leniently: `undefined` (or an empty
// Errors compare message/name/cause/errors leniently: `undefined` (or an empty
// expected message) on the expected side is ignored. An own `cause` on the
// expected error (even undefined) must exist on the actual error as well.
static bool errorSubset(JSC::JSGlobalObject* globalObject, JSC::MarkedArgumentBuffer& gcBuffer, CycleState& cycles, JSC::ThrowScope& scope, JSValue actual, JSValue expected)
Expand All @@ -568,9 +569,10 @@ static bool errorSubset(JSC::JSGlobalObject* globalObject, JSC::MarkedArgumentBu
JSC::JSObject* actualObject = actual.getObject();
JSC::JSObject* expectedObject = expected.getObject();

const JSC::Identifier keys[3] = {
const JSC::Identifier keys[4] = {
vm.propertyNames->message,
vm.propertyNames->name,
vm.propertyNames->cause,
JSC::Identifier::fromString(vm, "errors"_s),
};
for (const auto& key : keys) {
Expand All @@ -591,24 +593,15 @@ static bool errorSubset(JSC::JSGlobalObject* globalObject, JSC::MarkedArgumentBu
return false;
}

const JSC::Identifier causeIdentifier = JSC::Identifier::fromString(vm, "cause"_s);
JSC::PropertySlot expectedCauseSlot(expectedObject, JSC::PropertySlot::InternalMethodType::GetOwnProperty);
bool expectedHasCause = expectedObject->methodTable()->getOwnPropertySlot(expectedObject, globalObject, causeIdentifier, expectedCauseSlot);
bool expectedHasCause = expectedObject->methodTable()->getOwnPropertySlot(expectedObject, globalObject, vm.propertyNames->cause, expectedCauseSlot);
RETURN_IF_EXCEPTION(scope, false);
if (expectedHasCause) {
JSC::PropertySlot actualCauseSlot(actualObject, JSC::PropertySlot::InternalMethodType::GetOwnProperty);
bool actualHasCause = actualObject->methodTable()->getOwnPropertySlot(actualObject, globalObject, causeIdentifier, actualCauseSlot);
bool actualHasCause = actualObject->methodTable()->getOwnPropertySlot(actualObject, globalObject, vm.propertyNames->cause, actualCauseSlot);
RETURN_IF_EXCEPTION(scope, false);
if (!actualHasCause)
return false;
JSValue actualCause = actualObject->get(globalObject, causeIdentifier);
RETURN_IF_EXCEPTION(scope, false);
JSValue expectedCause = expectedObject->get(globalObject, causeIdentifier);
RETURN_IF_EXCEPTION(scope, false);
bool equal = compareBranch(globalObject, gcBuffer, cycles, scope, actualCause, expectedCause);
RETURN_IF_EXCEPTION(scope, false);
if (!equal)
return false;
}
return objectSubset(globalObject, gcBuffer, cycles, scope, actual, expected);
}
Expand Down Expand Up @@ -757,6 +750,18 @@ static bool compareBranch(JSC::JSGlobalObject* globalObject, JSC::MarkedArgument
return withCycleGuard(globalObject, gcBuffer, cycles, scope, actual, expected, objectSubset);
}

// DOMException is `instanceof Error`, so node compares it like an Error, but it is a DOM
// wrapper whose name/message are prototype accessors: the own-property walk at the bottom
// sees nothing to compare. Must stay ahead of the ErrorInstance arm and separate from it:
// node rejects DOMException vs Error on the Object.prototype.toString tag.
Comment thread
robobun marked this conversation as resolved.
Resolved
const bool actualIsDOMException = actual.isCell() && actual.asCell()->inherits<WebCore::JSDOMException>();
const bool expectedIsDOMException = expected.isCell() && expected.asCell()->inherits<WebCore::JSDOMException>();
if (actualIsDOMException || expectedIsDOMException) {
if (!actualIsDOMException || !expectedIsDOMException)
return false;
return withCycleGuard(globalObject, gcBuffer, cycles, scope, actual, expected, errorSubset);
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
}
Comment thread
claude[bot] marked this conversation as resolved.
Resolved

const bool actualIsError = actual.isCell() && actual.asCell()->inherits<JSC::ErrorInstance>();
const bool expectedIsError = expected.isCell() && expected.asCell()->inherits<JSC::ErrorInstance>();
if (actualIsError || expectedIsError) {
Expand Down
263 changes: 263 additions & 0 deletions test/js/node/assert/assert.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,267 @@ describe("assert.partialDeepStrictEqual", () => {
const arr = [[]];
assert.partialDeepStrictEqual([arr], arr);
});

// Node reads `cause` like message/name/errors (an undefined expected value is not compared)
// and additionally requires an own `cause` on expected to exist on actual.
// Expectations below were checked against Node v26.
describe("error cause", () => {
class InheritedCauseError extends Error {
get cause() {
return 1;
}
}

const rejected = [
[
"no actual cause vs own undefined expected cause",
() => new Error("x"),
() => new Error("x", { cause: undefined }),
],
[
"own undefined actual cause vs defined expected cause",
() => new Error("x", { cause: undefined }),
() => new Error("x", { cause: 1 }),
],
["different causes", () => new Error("x", { cause: 1 }), () => new Error("x", { cause: 2 })],
[
"actual cause differs from an inherited expected cause",
() => new Error("x", { cause: 2 }),
() => new InheritedCauseError("x"),
],
["no actual cause vs an inherited expected cause", () => new Error("x"), () => new InheritedCauseError("x")],
[
"inherited actual cause vs an own expected cause with the same value",
() => new InheritedCauseError("x"),
() => new Error("x", { cause: 1 }),
],
];

const accepted = [
[
"defined actual cause vs own undefined expected cause",
() => new Error("x", { cause: 1 }),
() => new Error("x", { cause: undefined }),
],
["same causes", () => new Error("x", { cause: 1 }), () => new Error("x", { cause: 1 })],
[
"expected cause is a subset of the actual cause",
() => new Error("x", { cause: { a: 1, b: 2 } }),
() => new Error("x", { cause: { a: 1 } }),
],
["actual cause vs no expected cause", () => new Error("x", { cause: 1 }), () => new Error("x")],
[
"own actual cause vs an inherited expected cause with the same value",
() => new Error("x", { cause: 1 }),
() => new InheritedCauseError("x"),
],
];

test.each(rejected)("rejects %s", (_name, actual, expected) => {
expect(() => assert.partialDeepStrictEqual(actual(), expected())).toThrow(assert.AssertionError);
});

test.each(accepted)("accepts %s", (_name, actual, expected) => {
assert.partialDeepStrictEqual(actual(), expected());
});
});

// DOMException is `instanceof Error` but has no own enumerable properties (name and message
// are prototype accessors), so node compares it like an Error: name, message and cause.
// Expectations below were checked against Node v26.
describe("DOMException", () => {
class SubException extends DOMException {}
const abortMessage = AbortSignal.abort().reason.message;

const rejected = [
[
"different name and message",
() => new DOMException("boom", "AbortError"),
() => new DOMException("other", "NotFoundError"),
],
[
"same name, different message",
() => new DOMException("boom", "AbortError"),
() => new DOMException("other", "AbortError"),
],
[
"same message, different name",
() => new DOMException("boom", "AbortError"),
() => new DOMException("boom", "NotFoundError"),
],
["default name vs explicit name", () => new DOMException("boom", "AbortError"), () => new DOMException("boom")],
[
"empty actual message vs non-empty expected message",
() => new DOMException("", "AbortError"),
() => new DOMException("boom", "AbortError"),
],
[
"different cause",
() => new DOMException("boom", { name: "AbortError", cause: 1 }),
() => new DOMException("boom", { name: "AbortError", cause: 2 }),
],
[
"expected cause has a property the actual cause lacks",
() => new DOMException("boom", { name: "AbortError", cause: { x: 1 } }),
() => new DOMException("boom", { name: "AbortError", cause: { x: 1, y: 2 } }),
],
[
"own cause on expected only",
() => new DOMException("boom", "AbortError"),
() => new DOMException("boom", { name: "AbortError", cause: 1 }),
],
[
"own undefined cause on expected only",
() => new DOMException("boom", "AbortError"),
() => new DOMException("boom", { name: "AbortError", cause: undefined }),
],
[
"own enumerable property on expected only",
() => new DOMException("boom", "AbortError"),
() => Object.assign(new DOMException("boom", "AbortError"), { extra: 1 }),
],
[
"subclass instances with different messages",
() => new SubException("boom", "SyntaxError"),
() => new SubException("other", "SyntaxError"),
],
[
"AbortSignal reason vs a different DOMException",
() => AbortSignal.abort().reason,
() => new DOMException("boom", "TimeoutError"),
],
[
"AbortSignal reason vs the same message under a different name",
() => AbortSignal.abort().reason,
() => new DOMException(abortMessage, "TimeoutError"),
],
["DOMException vs a plain object", () => new DOMException("boom", "AbortError"), () => ({})],
["plain object vs DOMException", () => ({}), () => new DOMException("boom", "AbortError")],
[
"DOMException vs an object with the same name and message",
() => new DOMException("boom", "AbortError"),
() => ({ name: "AbortError", message: "boom" }),
],
[
"DOMException vs an Error with the same name and message",
() => new DOMException("boom"),
() => new Error("boom"),
],
[
"Error vs a DOMException with the same name and message",
() => new Error("boom"),
() => new DOMException("boom"),
],
[
"nested in an object",
() => ({ error: new DOMException("boom", "AbortError") }),
() => ({ error: new DOMException("other", "NotFoundError") }),
],
[
"no array element matches",
() => [new DOMException("a", "AbortError"), new DOMException("b", "TimeoutError")],
() => [new DOMException("c", "TimeoutError")],
],
];

const accepted = [
[
"same name and message",
() => new DOMException("boom", "AbortError"),
() => new DOMException("boom", "AbortError"),
],
["both default name", () => new DOMException("boom"), () => new DOMException("boom")],
[
"empty expected message",
() => new DOMException("boom", "AbortError"),
() => new DOMException("", "AbortError"),
],
[
"same cause",
() => new DOMException("boom", { name: "AbortError", cause: 1 }),
() => new DOMException("boom", { name: "AbortError", cause: 1 }),
],
[
"expected cause is a subset of the actual cause",
() => new DOMException("boom", { name: "AbortError", cause: { x: 1, y: 2 } }),
() => new DOMException("boom", { name: "AbortError", cause: { x: 1 } }),
],
[
"own cause on actual only",
() => new DOMException("boom", { name: "AbortError", cause: 1 }),
() => new DOMException("boom", "AbortError"),
],
[
"own undefined cause on both",
() => new DOMException("boom", { name: "AbortError", cause: undefined }),
() => new DOMException("boom", { name: "AbortError", cause: undefined }),
],
[
"defined cause on actual, own undefined cause on expected",
() => new DOMException("boom", { name: "AbortError", cause: 1 }),
() => new DOMException("boom", { name: "AbortError", cause: undefined }),
],
[
"own enumerable property on actual only",
() => Object.assign(new DOMException("boom", "AbortError"), { extra: 1 }),
() => new DOMException("boom", "AbortError"),
],
[
"same own enumerable property on both",
() => Object.assign(new DOMException("boom", "AbortError"), { extra: 1 }),
() => Object.assign(new DOMException("boom", "AbortError"), { extra: 1 }),
],
[
"subclass instances with the same name and message",
() => new SubException("boom", "SyntaxError"),
() => new SubException("boom", "SyntaxError"),
],
[
"subclass instance vs base instance",
() => new SubException("boom", "SyntaxError"),
() => new DOMException("boom", "SyntaxError"),
],
[
"AbortSignal reason vs an equal DOMException",
() => AbortSignal.abort().reason,
() => new DOMException(abortMessage, "AbortError"),
],
[
"nested in an object",
() => ({ error: new DOMException("boom", "AbortError"), z: 1 }),
() => ({ error: new DOMException("boom", "AbortError") }),
],
[
"a later array element matches",
() => [new DOMException("a", "AbortError"), new DOMException("b", "TimeoutError")],
() => [new DOMException("b", "TimeoutError")],
],
[
"self-referencing causes on both sides",
() => {
const cause = {};
cause.self = cause;
return new DOMException("boom", { name: "AbortError", cause });
},
() => {
const cause = {};
cause.self = cause;
return new DOMException("boom", { name: "AbortError", cause });
},
],
];

test.each(rejected)("rejects %s", (_name, actual, expected) => {
expect(() => assert.partialDeepStrictEqual(actual(), expected())).toThrow(assert.AssertionError);
});

test.each(accepted)("accepts %s", (_name, actual, expected) => {
assert.partialDeepStrictEqual(actual(), expected());
});

test("the same instance on both sides is accepted", () => {
const error = new DOMException("boom", "AbortError");
assert.partialDeepStrictEqual(error, error);
});
});
});
Loading