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

improve-stubs-doc.md #2152

Merged
merged 1 commit into from Mar 25, 2020
Merged

improve-stubs-doc.md #2152

merged 1 commit into from Mar 25, 2020

Conversation

andrewdroz
Copy link

@andrewdroz andrewdroz commented Nov 5, 2019

#2062

Purpose (TL;DR) - mandatory

Improvement to stubs doc

  • Focus more on the basics of stubbing an object's function (stub pre-programmed to throw exception/return value)
  • Less discussion on spies
  • Provide very simple code to test as an example, instead of external library's (PubSub) code
  • Show why restoration of a stub is necessary

How to verify - mandatory

N.A. because no code change

Checklist for author

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

Copy link
Contributor

@fatso83 fatso83 left a comment

Choose a reason for hiding this comment

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

Hi, thanks for you contribution! While I can sympathize with the intent, unfortunately the execution doesn't go that well with the new work that has gone into making runnable examples. See all the work done on #2122 for the spies docs.

If there should be changes to the example code in the documentation, then the changes should be made so that they are actually runnable. The new example you posted would not pass this, as ./emailService is not an actual module.

Provide very simple code to test as an example, instead of external library's (PubSub) code

I don't see how it's actually easier? The interface of PubSub is already pretty simple, so any consumer could be made pretty simple as well. I think adjusting your example might end up being just as simple.

// VERIFY
expect(result).toBe("Error occurred in email client!");
// TEARDOWN
sendStub.restore();
Copy link
Contributor

Choose a reason for hiding this comment

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

As tests have a tendency to fail, it's better to have restore steps that will run irrespective of the failure status. This is better left to a afterEach hook.

it('should handle EmailClientError when emailService throws', function() {

// SETUP
let sendStub = sinon.stub(emailService, 'send').throws('EmailClientError');
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid let and other ES2015+ syntax in the examples. We try to have all produced be ES5.1 compatible.

@fatso83
Copy link
Contributor

fatso83 commented Dec 3, 2019

Is there something I can do to get the train back on its tracks?

@andrewdroz
Copy link
Author

andrewdroz commented Dec 5, 2019

Hello, sorry for not giving a reply thus far. Okay, I understand your points, and probably there's no need to remove the use of PubSub (I didn't really know what PubSub was about). I plan to try implementing runnable code this weekend, so I'll keep you posted.

@fatso83
Copy link
Contributor

fatso83 commented Dec 5, 2019

One shouldn't really need to know anything about the implementation of pubsub either. It's just an example of a very common pattern used practically everywhere: Publish/Subscribe (which is more or less the same as the Observer Pattern). As it usually just deals with two methods; publish({topic,msg}) and subscribe(topic) the concept is usually known to most, which is why I assume it was used to begin with.

@andrewdroz
Copy link
Author

I have replaced almost all the examples to be runnable in my latest change. There are 2 examples (yieldsTo, callArg) for which I had trouble making tests that would pass, perhaps due to me not fully understanding the two functions.

@fatso83 fatso83 self-assigned this Jan 15, 2020
@fatso83
Copy link
Contributor

fatso83 commented Jan 15, 2020

Wow, this seems great. I will need to check it out to understand what (if anything) is wrong with the 2 examples you mention, but in any case, this is a huge improvement.

@stale
Copy link

stale bot commented Mar 15, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Mar 15, 2020
@fatso83 fatso83 added the pinned label Mar 16, 2020
@stale stale bot removed the stale label Mar 16, 2020
@fatso83
Copy link
Contributor

fatso83 commented Mar 16, 2020

Ooh, bad me. I have forgotten about this ...

@fatso83
Copy link
Contributor

fatso83 commented Mar 16, 2020

I have looked into this and there was a test I could not get passing, spies-2-wrap-object-methods.stub. It always failed due to the sandbox not replacing all the fields with spies. This seems like a bug within Sinon, somehow, as changing the beforeEach to this made it pass:

           sandbox.spy(jQuery, 'getJSON');
           sandbox.spy(jQuery, 'ajax');
           //sandbox.spy(jQuery);

I will look some more into it.


EDIT: the example was at fault. jQuery is a function and so Sinon will try to wrap that function, not its fields.

@fatso83
Copy link
Contributor

fatso83 commented Mar 18, 2020

This is not abandoned, btw. I am actively working on a fork of this branch to get everything working, but I am struggling with a last test, as pubsub is throwing exceptions after the event loop has finished the current work (the test). So we are just talking amongst us about how to rewrite this to not cause Node (or Mocha) to exit with a non-zero exit code.

@fatso83
Copy link
Contributor

fatso83 commented Mar 18, 2020

Included this in #2239 (leaving your commit intact!), so when/if that gets merged, then so will this :)

@fatso83 fatso83 merged commit a504bd8 into sinonjs:master Mar 25, 2020
@fatso83
Copy link
Contributor

fatso83 commented Mar 25, 2020

Thanks for all the work, @andrewdroz! I had some additions as you can see in the PR that built upon this, but this should be live as soon as the next release is published.

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

Successfully merging this pull request may close these issues.

None yet

2 participants