Skip to content

Commit f096abf

Browse files
authored
fix (#2514): only force new or inherited descriptors to be configurable (#2515)
1 parent b2a4df5 commit f096abf

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/sinon/util/core/wrap-method.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,13 @@ module.exports = function wrapMethod(object, property, method) {
137137
for (i = 0; i < types.length; i++) {
138138
mirrorProperties(methodDesc[types[i]], wrappedMethodDesc[types[i]]);
139139
}
140-
methodDesc.configurable = true;
140+
141+
// you are not allowed to flip the configurable prop on an
142+
// existing descriptor to anything but false (#2514)
143+
if (!owned) {
144+
methodDesc.configurable = true;
145+
}
146+
141147
Object.defineProperty(object, property, methodDesc);
142148

143149
// catch failing assignment

test/issues/issues-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,4 +873,30 @@ describe("issues", function () {
873873
});
874874
});
875875
});
876+
877+
describe("#2514 (regression from fixing #2491) - writable object descriptors that are unconfigurable should be assignable", function () {
878+
function createInstanceWithWritableUconfigurablePropertyDescriptor() {
879+
const instance = {};
880+
Object.defineProperty(instance, "aMethod", {
881+
writable: true,
882+
configurable: false,
883+
value: function () {
884+
return 42;
885+
},
886+
});
887+
888+
return instance;
889+
}
890+
891+
it("should be able to assign and restore unconfigurable descriptors that are writable", function () {
892+
const o =
893+
createInstanceWithWritableUconfigurablePropertyDescriptor();
894+
895+
refute.exception(() =>
896+
this.sandbox.stub(o, "aMethod").returns("stubbed")
897+
);
898+
assert.equals("stubbed", o.aMethod());
899+
this.sandbox.restore();
900+
});
901+
});
876902
});

0 commit comments

Comments
 (0)