Skip to content

Commit 4640a91

Browse files
committedJan 10, 2024
[Fix] intercept: give a proper error message with a readonly Symbol property
1 parent af4d109 commit 4640a91

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
 

‎lib/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ Test.prototype.intercept = function intercept(obj, property) {
348348
$push(calls, { type: 'set', success: !!canSet, value: value, args: args, receiver: this });
349349

350350
if (!canSet && strictMode) {
351-
throw new TypeError('Cannot assign to read only property \'' + property + '\' of object \'' + inspect(obj) + '\'');
351+
throw new TypeError('Cannot assign to read only property `' + inspect(property) + '` of object `' + inspect(obj) + '`');
352352
}
353353
return value;
354354
}

‎test/intercept.js

+25
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ tap.test('intercept: output', function (tt) {
4343
'ok ' + ++count + ' undefined is returned from Get with .call',
4444
'ok ' + ++count + ' foo2: nonwritable property throws on Set',
4545
'ok ' + ++count + ' undefined is still returned from Get',
46+
v.hasSymbols ? [
47+
'ok ' + ++count + ' nonwritable Symbol property throws on Set',
48+
'ok ' + ++count + ' undefined is still returned from Get of a Symbol'
49+
] : [
50+
'ok ' + ++count + ' undefined is still returned from Get of a Symbol # SKIP no Symbol support'
51+
],
4652
'ok ' + ++count + ' throwing get implementation throws',
4753
'ok ' + ++count + ' throwing get implementation throws with .call',
4854
'ok ' + ++count + ' throwing set implementation throws',
@@ -166,6 +172,25 @@ tap.test('intercept: output', function (tt) {
166172
st.equal(o.foo2, undefined, 'undefined is still returned from Get');
167173
results2.restore();
168174

175+
if (v.hasSymbols) {
176+
var sym = Symbol('fooSymbol');
177+
var resultsSymbol = st.intercept(
178+
o,
179+
sym,
180+
{ __proto__: null, writable: false },
181+
true
182+
);
183+
st.throws(
184+
function () { o[sym] = 42; },
185+
new RegExp('^TypeError: Cannot assign to read only property `Symbol\\(fooSymbol\\)` of object `' + inspect(o) + '`$'),
186+
'nonwritable Symbol property throws on Set'
187+
);
188+
st.equal(o[sym], undefined, 'undefined is still returned from Get of a Symbol');
189+
resultsSymbol.restore();
190+
} else {
191+
st.equal(undefined, undefined, 'undefined is still returned from Get of a Symbol', { skip: 'no Symbol support' });
192+
}
193+
169194
var resultsThrowGet = st.intercept(o, 'fooThrowGet', { get: function () { throw up; } });
170195
st.throws(
171196
function () { return o.fooThrowGet; },

0 commit comments

Comments
 (0)
Please sign in to comment.