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

Add firstArg to spy calls and fakes. #2150

Merged
merged 2 commits into from Feb 14, 2020

Conversation

GreeKatrina
Copy link
Contributor

@GreeKatrina GreeKatrina commented Oct 30, 2019

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.

var spy = sinon.spy();
var date = new Date();

spy(date, 1, 2);

spy.lastCall.firstArg === date;
// true

fake.firstArg

This property is a convenient way to get a reference to the first argument passed in the last call to the fake.

var f = sinon.fake();
var date1 = new Date();
var date2 = new Date();

f(date1, 1, 2);
f(date2, 1, 2);

f.firstArg === date2;
// true

How to verify

  1. Check out this branch
  2. npm install
  3. npm test

Checklist for author

  • npm run lint passes
  • References to standard library functions are cached.

Copy link
Member

@mroderick mroderick left a 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 🎉

Copy link
Member

@mantoni mantoni left a 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.

@GreeKatrina
Copy link
Contributor Author

@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.

assert.equals(spy.getCall(2).firstArg, 46);

spy();
assert.equals(spy.getCall(3).firstArg, undefined);
Copy link
Member

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.

Copy link
Contributor Author

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);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe assert.isTrue?

Copy link
Contributor Author

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;
Copy link
Member

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.

Copy link
Contributor Author

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.)

Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Member

@mantoni mantoni left a 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.

@fearphage
Copy link
Member

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?

@mantoni mantoni merged commit 9d4a01b into sinonjs:master Feb 14, 2020
@mroderick
Copy link
Member

This has been published as part of sinon@9.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants