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

feat: throw when trying to use .value() on a method #2631

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
npm run unimported

browser-test:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down
15 changes: 15 additions & 0 deletions lib/sinon/default-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const arrayProto = require("@sinonjs/commons").prototypes.array;
const isPropertyConfigurable = require("./util/core/is-property-configurable");
const exportAsyncBehaviors = require("./util/core/export-async-behaviors");
const extend = require("./util/core/extend");
const getPropertyDescriptor = require("./util/core/get-property-descriptor");

const slice = arrayProto.slice;

Expand Down Expand Up @@ -286,6 +287,20 @@ const defaultBehaviors = {

value: function value(fake, newVal) {
const rootStub = fake.stub || fake;
const propertyDescriptor = getPropertyDescriptor(
rootStub.rootObj,
rootStub.propName,
);
const propertyIsGetter = propertyDescriptor.get !== undefined;
const propertyIsMethod =
!propertyIsGetter &&
typeof rootStub.rootObj[rootStub.propName] === "function";

if (propertyIsMethod) {
throw new Error(
`${rootStub.propName} is a function, not a getter or value. Use .returns() instead of .value()`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we supposed to allow setting .value() on a getter property? Seems erronous.

Copy link
Member Author

@mroderick mroderick Jan 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure either ... to me it seemed more reasonable to use .returns() rather than .value() on a getter. I'm happy to change it to do that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just do the simplest thing: prevent using value() on a method. So "${rootStub.propName} is a function and should not be overridden by .value(). Use .returns() when creating a function that returns a value.

There is little reason to add synthetic property getters and setters to the mix, as it just adds more noise/possible confusion, IMHO.

);
}

Object.defineProperty(rootStub.rootObj, rootStub.propName, {
value: newVal,
Expand Down
23 changes: 23 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3781,6 +3781,29 @@ describe("stub", function () {
assert.equals(myFunc.prop, "rawString");
});

it("allows stubbing getters", function () {
const y = {
get foo() {
return "bar";
},
};
refute.exception(function () {
createStub(y, "foo").value("bar");
});
});
Comment on lines +3784 to +3793
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original issue (#2629) does not deal with this issue, so this seems more like we are documenting existing (non-intentional) behavior? Dan does talk about getters, but that is the conventional Java-bean like getters, which are methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we change it like in #2631 (comment), then this test will obviously also need to be changed


it("disallows stubbing non-accessor methods", function () {
const x = {
getFoo: function getFoo() {
return "bar";
},
};

assert.exception(function () {
createStub(x, "getFoo").value("baz");
}, "Error: getFoo is a function, not a getter or value. Use .returns() instead of .value()");
});

it("allows stubbing object props with configurable false", function () {
const myObj = {};
Object.defineProperty(myObj, "prop", {
Expand Down
Loading