Skip to content

Commit

Permalink
fix (#2514): only force new or inherited descriptors to be configurab…
Browse files Browse the repository at this point in the history
…le (#2515)
  • Loading branch information
fatso83 committed May 18, 2023
1 parent b2a4df5 commit f096abf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/sinon/util/core/wrap-method.js
Expand Up @@ -137,7 +137,13 @@ module.exports = function wrapMethod(object, property, method) {
for (i = 0; i < types.length; i++) {
mirrorProperties(methodDesc[types[i]], wrappedMethodDesc[types[i]]);
}
methodDesc.configurable = true;

// you are not allowed to flip the configurable prop on an
// existing descriptor to anything but false (#2514)
if (!owned) {
methodDesc.configurable = true;
}

Object.defineProperty(object, property, methodDesc);

// catch failing assignment
Expand Down
26 changes: 26 additions & 0 deletions test/issues/issues-test.js
Expand Up @@ -873,4 +873,30 @@ describe("issues", function () {
});
});
});

describe("#2514 (regression from fixing #2491) - writable object descriptors that are unconfigurable should be assignable", function () {
function createInstanceWithWritableUconfigurablePropertyDescriptor() {
const instance = {};
Object.defineProperty(instance, "aMethod", {
writable: true,
configurable: false,
value: function () {
return 42;
},
});

return instance;
}

it("should be able to assign and restore unconfigurable descriptors that are writable", function () {
const o =
createInstanceWithWritableUconfigurablePropertyDescriptor();

refute.exception(() =>
this.sandbox.stub(o, "aMethod").returns("stubbed")
);
assert.equals("stubbed", o.aMethod());
this.sandbox.restore();
});
});
});

0 comments on commit f096abf

Please sign in to comment.