Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions core/engine/src/builtins/weak_set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,6 @@ impl WeakSet {
Ok(set.contains_key(value.inner()).into())
}
}

#[cfg(test)]
mod tests;
94 changes: 94 additions & 0 deletions core/engine/src/builtins/weak_set/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use crate::{JsNativeErrorKind, TestAction, run_test_actions};

#[test]
fn weakset_add_and_has() {
run_test_actions([
TestAction::run("var ws = new WeakSet(); var obj = {}; ws.add(obj);"),
TestAction::assert("ws.has(obj)"),
]);
}

#[test]
fn weakset_add_chaining() {
run_test_actions([
TestAction::run("var ws = new WeakSet(); var obj = {};"),
TestAction::assert("ws.add(obj) === ws"),
]);
}

#[test]
fn weakset_delete_behavior() {
run_test_actions([
TestAction::run("var ws = new WeakSet(); var obj = {}; ws.add(obj);"),
TestAction::assert("ws.delete(obj) === true"),
TestAction::assert("ws.delete(obj) === false"),
TestAction::assert("ws.has(obj) === false"),
]);
}

#[test]
fn weakset_add_primitive_rejection() {
run_test_actions([
TestAction::run("var ws = new WeakSet();"),
TestAction::assert_native_error(
"ws.add(1);",
JsNativeErrorKind::Type,
"WeakSet.add: expected target argument of type `object`, got target of type `number`",
),
TestAction::assert_native_error(
"ws.add('x');",
JsNativeErrorKind::Type,
"WeakSet.add: expected target argument of type `object`, got target of type `string`",
),
TestAction::assert_native_error(
"ws.add(true);",
JsNativeErrorKind::Type,
"WeakSet.add: expected target argument of type `object`, got target of type `boolean`",
),
TestAction::assert_native_error(
"ws.add(null);",
JsNativeErrorKind::Type,
"WeakSet.add: expected target argument of type `object`, got target of type `object`",
Comment on lines +48 to +51
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null case asserts an error message where the "got" type is object (because typeof null is "object"), which makes the message read like there was no type mismatch ("expected object … got object"). This makes the test misleading/brittle and also locks in a confusing engine error string. Consider either (a) updating the WeakSet.add TypeError message to special-case null (e.g., report null explicitly) and then adjusting this assertion accordingly, or (b) changing this test to assert only that a TypeError is thrown for null without coupling to the exact message text.

Suggested change
TestAction::assert_native_error(
"ws.add(null);",
JsNativeErrorKind::Type,
"WeakSet.add: expected target argument of type `object`, got target of type `object`",
TestAction::assert(
"try { ws.add(null); false } catch (e) { e instanceof TypeError }",

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine, we'll fix it eventually

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rn I was working on this fix only should I push it 🙆‍♂️!?

),
TestAction::assert_native_error(
"ws.add(undefined);",
JsNativeErrorKind::Type,
"WeakSet.add: expected target argument of type `object`, got target of type `undefined`",
),
TestAction::assert_native_error(
"ws.add(Symbol('id'));",
JsNativeErrorKind::Type,
"WeakSet.add: expected target argument of type `object`, got target of type `symbol`",
),
]);
}

#[test]
fn weakset_has_primitive_returns_false() {
run_test_actions([
TestAction::run("var ws = new WeakSet();"),
TestAction::assert("ws.has(1) === false"),
TestAction::assert("ws.has('x') === false"),
TestAction::assert("ws.has(null) === false"),
]);
}

#[test]
fn weakset_delete_primitive_returns_false() {
run_test_actions([
TestAction::run("var ws = new WeakSet();"),
TestAction::assert("ws.delete(1) === false"),
TestAction::assert("ws.delete('x') === false"),
TestAction::assert("ws.delete(null) === false"),
]);
}

#[test]
fn weakset_add_duplicate() {
run_test_actions([
TestAction::run("var ws = new WeakSet(); var obj = {}; ws.add(obj);"),
TestAction::assert("ws.has(obj) === true"),
TestAction::run("ws.add(obj);"),
TestAction::assert("ws.has(obj) === true"),
]);
}
Loading