karatelabs/karate · error · TypeError
Invalid value used in weak set
Error message
Invalid value used in weak set
What it means
WeakSet can only store values eligible for weak references — objects (and registered symbols). Karate's JsWeakSet.addValue checks JsWeakMap.canBeHeldWeakly(value) and throws a TypeError when add() receives a primitive, matching V8's 'Invalid value used in weak set'.
Solutions
- Add only objects/functions: weakSet.add(obj).
- For primitive values use a regular Set instead of WeakSet.
- Guard before adding: if (value && typeof value === 'object') weakSet.add(value).
- Wrap primitives in holder objects if weak semantics are genuinely needed: weakSet.add({ v: 42 }).
Example fix
// before const seen = new WeakSet(); seen.add(id); // id is a string // after const seen = new Set(); seen.add(id);
Defensive patterns
Strategy: validation
Validate before calling
function isWeakValue(v) { return (typeof v === 'object' && v !== null) || typeof v === 'function'; }
if (!isWeakValue(value)) throw new Error('WeakSet values must be objects');
ws.add(value); Type guard
function isWeakValue(v) { return (typeof v === 'object' && v !== null) || typeof v === 'function'; } Prevention
- Use Set for primitive values
- Check that values are not null/undefined before add()
- Be careful with JSON-parsed arrays — elements are primitives
When it happens
Trigger: weakSet.add(42), weakSet.add('str'), weakSet.add(null), weakSet.add(undefined), or weakSet.add(Symbol('local')) — any non-object, non-function value passed to add().
Common situations: Deduplicating IDs that are strings/numbers rather than objects; adding array elements from JSON-parsed data (all primitives); variables that are unexpectedly undefined at call time.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Constructor WeakSet requires 'new'
- WeakSet.prototype.add is not callable
- Method WeakSet.prototype called on incompatible receiver
- Cannot set property ' ' which has only a getter
- assignment to constant
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/587d5c7874e210e4.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsWeakSet.java:49
* identity-keyed.
*/
class JsWeakSet extends JsObject {
private static final Object PRESENT = new Object();
private final IdentityHashMap<Object, Object> elements = new IdentityHashMap<>();
JsWeakSet() {
super(null, JsWeakSetPrototype.INSTANCE);
}
boolean has(Object value) {
return JsWeakMap.canBeHeldWeakly(value) && elements.containsKey(value);
}
void addValue(Object value) {
if (!JsWeakMap.canBeHeldWeakly(value)) {
throw JsErrorException.typeError("Invalid value used in weak set");
}
elements.put(value, PRESENT);
}
boolean deleteValue(Object value) {
if (!has(value)) {
return false;
}
elements.remove(value);
return true;
}
}
View on GitHub (pinned to a22eb90246)