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
Add firstArg to spy calls and fakes. #2150
Add firstArg to spy calls and fakes. #2150
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your very first pull request to Sinon 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice pull request. Thank you! The changes to the documentation should be made in docs/release-source
though.
@mantoni Ah, gotcha. That makes sense. There are a lot of files in that folder so I probably missed it haha. I'll update it and let you know. |
10c6bb3
to
068757d
Compare
assert.equals(spy.getCall(2).firstArg, 46); | ||
|
||
spy(); | ||
assert.equals(spy.getCall(3).firstArg, undefined); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use assert.isUndefined
here, as you did in fake-test.js
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just copied the tests for lastArg
. I can change it though if you'd like.
assert.equals(f.firstArg, 47); | ||
|
||
f(true, 47, "string", false); | ||
assert.equals(f.firstArg, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe assert.isTrue
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also copied the tests for lastArg
here as well. Happy to change them though.
@@ -123,6 +123,7 @@ var proxyApi = { | |||
this.thirdCall = null; | |||
this.lastCall = null; | |||
this.args = []; | |||
this.firstArg = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit weird/inconsistent that the values start out as undefined
, but become null
after being reset()
.
Not sure this needs to be fixed, but just making note of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed this too. I was going to change it, but didn't want to assume whether the values should be undefined
or null
. (My vote is undefined
.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the idea of returning to undefined
. Do you want to make the change, before we merge this in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Did you just want me to change this line to undefined
? I feel like it'd be weird to have firstArg
and lastArg
default to different values, but changing lastArg
's default value is a big change. Maybe we could do that in a separate PR and just change firstArg
to default to undefined
for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Let's leave lastArg
unchanged in this PR.
A separate PR to tidy up the initial and reset values would be most welcome, and will trigger a new major release.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work. The minor suggestions to change the assertions shouldn't block merging.
Should we merge this and fix the minor assertion issues in another PR? |
This has been published as part of |
Purpose - Add
firstArg
convenience property to fakes and spy calls.Currently, we allow getting the last argument for spy calls and fakes. It would be nice to be able to get the first arg without doing
args[0]
. I couldn't find any open issues or feature requests for this.Solution
spyCall.firstArg
This property is a convenience for the first argument of the call.
fake.firstArg
This property is a convenient way to get a reference to the first argument passed in the last call to the fake.
How to verify
npm install
npm test
Checklist for author
npm run lint
passes