Skip to content

Commit

Permalink
[Fix] intercept: give a proper error message with a readonly Symbol…
Browse files Browse the repository at this point in the history
… property
  • Loading branch information
ljharb committed Jan 10, 2024
1 parent af4d109 commit 4640a91
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/test.js
Expand Up @@ -348,7 +348,7 @@ Test.prototype.intercept = function intercept(obj, property) {
$push(calls, { type: 'set', success: !!canSet, value: value, args: args, receiver: this });

if (!canSet && strictMode) {
throw new TypeError('Cannot assign to read only property \'' + property + '\' of object \'' + inspect(obj) + '\'');
throw new TypeError('Cannot assign to read only property `' + inspect(property) + '` of object `' + inspect(obj) + '`');
}
return value;
}
Expand Down
25 changes: 25 additions & 0 deletions test/intercept.js
Expand Up @@ -43,6 +43,12 @@ tap.test('intercept: output', function (tt) {
'ok ' + ++count + ' undefined is returned from Get with .call',
'ok ' + ++count + ' foo2: nonwritable property throws on Set',
'ok ' + ++count + ' undefined is still returned from Get',
v.hasSymbols ? [
'ok ' + ++count + ' nonwritable Symbol property throws on Set',
'ok ' + ++count + ' undefined is still returned from Get of a Symbol'
] : [
'ok ' + ++count + ' undefined is still returned from Get of a Symbol # SKIP no Symbol support'
],
'ok ' + ++count + ' throwing get implementation throws',
'ok ' + ++count + ' throwing get implementation throws with .call',
'ok ' + ++count + ' throwing set implementation throws',
Expand Down Expand Up @@ -166,6 +172,25 @@ tap.test('intercept: output', function (tt) {
st.equal(o.foo2, undefined, 'undefined is still returned from Get');
results2.restore();

if (v.hasSymbols) {
var sym = Symbol('fooSymbol');
var resultsSymbol = st.intercept(
o,
sym,
{ __proto__: null, writable: false },
true
);
st.throws(
function () { o[sym] = 42; },
new RegExp('^TypeError: Cannot assign to read only property `Symbol\\(fooSymbol\\)` of object `' + inspect(o) + '`$'),
'nonwritable Symbol property throws on Set'
);
st.equal(o[sym], undefined, 'undefined is still returned from Get of a Symbol');
resultsSymbol.restore();
} else {
st.equal(undefined, undefined, 'undefined is still returned from Get of a Symbol', { skip: 'no Symbol support' });
}

var resultsThrowGet = st.intercept(o, 'fooThrowGet', { get: function () { throw up; } });
st.throws(
function () { return o.fooThrowGet; },
Expand Down

0 comments on commit 4640a91

Please sign in to comment.