Skip to content

Implement throws and mimicks behaviour fix - #80

Merged
ffMathy merged 5 commits into
masterfrom
implement-throws
Feb 21, 2020
Merged

Implement throws and mimicks behaviour fix#80
ffMathy merged 5 commits into
masterfrom
implement-throws

Conversation

@notanengineercom

@notanengineercom notanengineercom commented Feb 19, 2020

Copy link
Copy Markdown
Collaborator

Closes #67

  • Implements .throwsmethod
  • Fixes .mimicks ignoring arguments - the mimicks function was always being executed, the arguments that should have been matched were ignored

It makes me a bit uncomfortable to give.throws an argument of type any, but according to the js spec, anything can be thrown.
Documentation for .throws needs to be added


I would like to refactor (not in this pr) a bit the code, mostly name changes, grouping stuff and cleaner tests. To start to work on that, I really think it would be great to add lint rules, to have all the code of this repository with the same format. Do you have any lint preferences?

@notanengineercom notanengineercom added enhancement New feature or request question Further information is requested wip Work in progress - should not merge labels Feb 19, 2020
@ffMathy

ffMathy commented Feb 20, 2020

Copy link
Copy Markdown
Owner

I don't have any lint preferences, just don't make it too strict in my opinion.

Wow, this is awesome! Only thing missing is documentation and an example in the readme file! Code in libraries is only as good as its documentation.

Will merge right away when there's documentation! 👍👍👍

@ffMathy

ffMathy commented Feb 20, 2020

Copy link
Copy Markdown
Owner

Actually, at second thought... We should mimick how NSubstitute throws exceptions, since this is a port of that library:

https://nsubstitute.github.io/help/throwing-exceptions/

Do you think that is possible in TypeScript?

Ironically I think your approach is actually better, but consistency is also quite important.

Documentation for throwing exceptions should still be there though.

@notanengineercom

Copy link
Copy Markdown
Collaborator Author

I don't have any lint preferences, just don't make it too strict in my opinion.

Hahaha, I’ll ask only this -> Semicolon yes or no?

—-
I will take a look on the NSubstitute reference, and see what we can do. Currently I’m using mimicks to throw errors, but it feels quite hacky. If I recall correctly with .returns it was not possible as it takes always the arguments of the mocked property. But maybe it’s possible to have an override for returns, where it takes one argument of type Function => never

@ffMathy

ffMathy commented Feb 20, 2020

Copy link
Copy Markdown
Owner

Semicolon definitely yes 😂

Alright. Mimicks is a substitute.js feature only anyway, so maybe it isn't that bad.

Feel free to give it a shot. If we can't do it, we'll merge this anyway.

But documentation is needed in either case 👍🙇

@notanengineercom

Copy link
Copy Markdown
Collaborator Author

Hahaha that's what I imagined!

So, from what I saw, in NSubstitute when using .returns, you can also return a callback. With that, the possibility to throw an exception is achieved. But that's not possible with the current implementation of SusbtituteJS. It seems to me that .mimicks was created for that purpose?
I modified .returnsso if a return value is a callback, it gets called, but that would be a breaking change -> current implementations of .returnsreturning a function would be called before being returned.

So the options we have is:

  1. We go with this pr's implementation - clear usecase distinction
  2. We don't change anything and use .mimicks
  3. We modify .returns to accept callbacks - breaking change

On any of the scenarios documentation will be added. I haven't written any, as we didn't decide where to go yet.
I prefer implementation 1, but maybe you prefer the 2nd or 3rd!

@ffMathy

ffMathy commented Feb 21, 2020

Copy link
Copy Markdown
Owner

Alright, let's go with the original PR then. That's also very easy to understand.

@notanengineercom

Copy link
Copy Markdown
Collaborator Author

Cool, I will add the documentation for the throws function!

@notanengineercom

Copy link
Copy Markdown
Collaborator Author

There is the documentation, feel free to modify it or let me know if I should add something more!

@ffMathy
ffMathy merged commit edc0734 into master Feb 21, 2020
@ffMathy

ffMathy commented Feb 21, 2020

Copy link
Copy Markdown
Owner

Thanks!

@notanengineercom
notanengineercom deleted the implement-throws branch February 24, 2020 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request question Further information is requested wip Work in progress - should not merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to mock that a function throws an Error

2 participants