Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only force new or inherited descriptors to be configurable #2515

Merged
merged 1 commit into from May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
});
});
});