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

Fix 2377: Throw error when trying to stub non-configurable or non-writable properties #2417

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ function stub(object, property) {
}

var actualDescriptor = getPropertyDescriptor(object, property);

assertValidPropertyDescriptor(actualDescriptor, property);

var isObjectOrFunction =
typeof object === "object" || typeof object === "function";
var isStubbingEntireObject =
Expand Down Expand Up @@ -147,6 +150,25 @@ stub.createStubInstance = function (constructor, overrides) {
return stubbedObject;
};

function assertValidPropertyDescriptor(descriptor, property) {
if (!descriptor || !property) {
return;
}
if (!descriptor.configurable && !descriptor.writable) {
throw new TypeError(`Descriptor for property ${property} is non-configurable and non-writable`);
}
if ((descriptor.get || descriptor.set) && !descriptor.configurable) {
throw new TypeError(`Descriptor for accessor property ${property} is non-configurable`);
}
if (isDataDescriptor(descriptor) && !descriptor.writable) {
throw new TypeError(`Descriptor for data property ${property} is non-writable`);
}
}

function isDataDescriptor(descriptor) {
return !descriptor.value && !descriptor.writable && !descriptor.set && !descriptor.get;
}

/*eslint-disable no-use-before-define*/
function getParentBehaviour(stubInstance) {
return stubInstance.parent && getCurrentBehavior(stubInstance.parent);
Expand Down
Loading