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

syntax sugar for resolvesArg issue-1665 #1846

Merged
merged 5 commits into from Jul 3, 2018
Merged

Conversation

huttli
Copy link
Contributor

@huttli huttli commented Jun 20, 2018

Purpose (TL;DR) - mandatory

Fix issue #1665

Background (Problem in detail) - optional

I'm well aware that the issue was closed in favour of workarounds with existing syntax. But I can argue that issue #804 falls into the same category as:
they're doable with a few more keystrokes.
they're syntax sugars.

Basically we're saving the effort of writing:
stub.callsFake(async (params) => params)
into
stub.resolvesArg(0)

It makes sense to have a resolvesArg given that we already had returnsArg and throwsArg.

How to verify - mandatory

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

Checklist for author

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

@coveralls
Copy link

coveralls commented Jun 20, 2018

Pull Request Test Coverage Report for Build 2554

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 59 unchanged lines in 4 files lost coverage.
  • Overall coverage decreased (-0.6%) to 84.715%

Files with Coverage Reduction New Missed Lines %
sinon/stub.js 6 82.58%
sinon/sandbox.js 15 86.4%
sinon/behavior.js 19 60.4%
sinon/default-behaviors.js 19 34.05%
Totals Coverage Status
Change from base Build 2534: -0.6%
Covered Lines: 1782
Relevant Lines: 2031

💛 - Coveralls

Copy link
Member

@fearphage fearphage left a comment

Choose a reason for hiding this comment

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

It looks good. I have few suggestions though.

var stub = createStub.create();
stub.resolvesArg(2);

assert.equals(resolveSpy.callCount, 0);
Copy link
Member

Choose a reason for hiding this comment

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

assert(resolveSpy.notCalled)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -165,6 +165,21 @@ module.exports = {
fake.fakeFn = undefined;
},

resolvesArg: function resolvesArg(fake, index) {
if (typeof index !== "number") {
Copy link
Member

Choose a reason for hiding this comment

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

Add a test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

var stub = createStub.create();
stub.rejects(Error("should be superseeded")).resolvesArg(1);

return stub().then();
Copy link
Member

Choose a reason for hiding this comment

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

Add some meaningful assertion to this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -137,6 +138,8 @@ var proto = {
throw args[this.throwArgAt];
} else if (this.fakeFn) {
return this.fakeFn.apply(context, args);
} else if (typeof this.resolveArgAt === "number") {
return (this.promiseLibrary || Promise).resolve(args[this.resolveArgAt]);
Copy link
Member

Choose a reason for hiding this comment

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

Will you make this more strict (like throwsArg)? By default this will resolve to undefined, but I think it should throw if there are not enough arguments. Having it resolve to an nonexistent argument doesn't seem like what people expect when writing tests.

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 did the old check just like throwArgAt in this pull request.
I'm happy to use that ensureArgs if PR #1848 get merged first and uses the correct argument index number.

Copy link
Member

@fearphage fearphage left a comment

Choose a reason for hiding this comment

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

Remove a duplicate test and it's good to go from end.

assert(resolveSpy.notCalled);
});

it("throws if index is not a number", function () {
Copy link
Member

Choose a reason for hiding this comment

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

This test and the one below are essentially the same test. String and undefined are both not Numbers. I'd keep this test label/message, but use the test below (using undefined) personally. I think that's the test throwsArg uses.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@fearphage fearphage closed this Jun 27, 2018
@fearphage fearphage reopened this Jun 27, 2018
@mroderick mroderick added the semver:minor changes will cause a new minor version label Jun 28, 2018
@mroderick
Copy link
Member

This pull request expands the API. For that to be useful, people need to know about it.

Please document this new feature in the stub documentation in docs/release-source.

@huttli
Copy link
Contributor Author

huttli commented Jun 28, 2018

I've done the doc update with a since api version 6.1.0 after @mroderick 's semver:minor label. I'll update if this is not the case.

@mroderick mroderick merged commit 2d27d4d into sinonjs:master Jul 3, 2018
@mroderick
Copy link
Member

This has been published to NPM as sinon@6.1.0. Thank you 🙇

franck-romano pushed a commit to franck-romano/sinon that referenced this pull request Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver:minor changes will cause a new minor version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants