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

Added error handling in hook callback #21

Merged
merged 1 commit into from
Mar 31, 2019
Merged

Added error handling in hook callback #21

merged 1 commit into from
Mar 31, 2019

Conversation

mpeyper
Copy link
Member

@mpeyper mpeyper commented Mar 26, 2019

What:

Capture errors that are thrown by hooks so they can be asserted in tests and are more visible in test failures.

Why:

Resolves #20

How:

  • Added result.error that returns any thrown value from the hook callback
  • Changed result.current to be getter that can throw result.error if value is not valid
    • Happy side-effect is that result.current is no readonly and cannot be overridden by users

Checklist:

  • Documentation updated
  • Tests
  • Typescript definitions updated
  • Ready to be merged
  • Added myself to contributors table

I'll comment the parts I'm not sure about for review.

@codecov-io
Copy link

Codecov Report

Merging #21 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #21   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           1      1           
  Lines          18     28   +10     
  Branches        0      1    +1     
=====================================
+ Hits           18     28   +10
Impacted Files Coverage Δ
src/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e13623d...80a53ef. Read the comment docs.

@@ -138,6 +138,7 @@ Renders a test component that will call the provided `callback`, including any h

- `result` (`object`)
- `current` (`any`) - the return value of the `callback` function
- `error` (`Error`) - the error that was thrown if the `callback` function threw an error during rendering
Copy link
Member Author

Choose a reason for hiding this comment

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

Technically you can throw anything (not just an Error instance), so I'm not sure if this should be any as well?

Choose a reason for hiding this comment

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

I am not knowledgeable enough with typescript, but I think even if someone would throw, it would be an instance of Error as they would extend Error to create a custom error in case they wanted to. So, I think this should be fine unless someone with knowledge of typescript differs in opinion.

@@ -7,7 +7,8 @@ export function renderHook<P, R>(
} & RenderOptions
): {
readonly result: {
current: R
readonly current: R,
readonly error: Error
Copy link
Member Author

Choose a reason for hiding this comment

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

Again, technically you can throw anything, so maybe this should be a bit looser?

Also, typescript isn't really my thing, so not sure if readonly usage is correct here and the fact that either current or error will be undefined depending on whether the hook threw or not (I think types are nullable/undefinedable by default in Typescript?)?

expect(result.current).not.toBe(undefined)
expect(result.error).toBe(undefined)
})
})
Copy link
Member Author

@mpeyper mpeyper Mar 26, 2019

Choose a reason for hiding this comment

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

Are there any other test cases or usage patterns we'd want to to see being tested?

@mpeyper
Copy link
Member Author

mpeyper commented Mar 26, 2019

@dhruv-m-patel I'd be interested in your review and feedback on this one.

@otofu-square likewise for the type definition changes.

Copy link

@dhruv-m-patel dhruv-m-patel left a comment

Choose a reason for hiding this comment

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

I pulled the changes using git+ssh://git@github.com:mpeyper/react-hooks-testing-library#error-updates and verified this to work as outlined in your tests.

This is the first time I ever posted an issue on a package I am using and the maintainer/owner showed the willingness to go an extra mile and improve the package. This is very much appreciated. Many thanks!

@mpeyper
Copy link
Member Author

mpeyper commented Mar 26, 2019

@dhruv-m-patel please submit a PR adding yourself as a contributor with a review contribution.

This is the first time I ever posted an issue on a package I am using and the maintainer/owner showed the willingness to go an extra mile and improve the package. This is very much appreciated. Many thanks!

I hope you continue to involve yourself in OSS despite the previous bad experiences. As maintainers, sometimes requests go against the core principal's of the package or are just too difficult/time consuming to justify a niche use case, but where possible we should always be willing to consider new ideas for the community.

@mpeyper
Copy link
Member Author

mpeyper commented Mar 26, 2019

@jpeyper I feel like you should get some sort of contribution recognition for this as wel, given the idea to use a getter was 100% yours.

@jpeyper
Copy link
Contributor

jpeyper commented Mar 26, 2019

@jpeyper I feel like you should get some sort of contribution recognition for this as wel, given the idea to use a getter was 100% yours.

Thanks :)

@dhruv-m-patel dhruv-m-patel mentioned this pull request Mar 27, 2019
5 tasks
@dhruv-m-patel
Copy link

I hope you continue to involve yourself in OSS despite the previous bad experiences.

Thanks for encouraging.

we should always be willing to consider new ideas for the community.

definitely 👍

please submit a PR adding yourself as a contributor with a review contribution.

Done!

@mpeyper
Copy link
Member Author

mpeyper commented Mar 27, 2019

I'd really like to get someone with more experience with Typescript to tell me I'm not doing something dumb with the types before I merge this.

@mpeyper
Copy link
Member Author

mpeyper commented Mar 31, 2019

Well, I don't think I'm going to get that Typescript review I'm after, so I guess we'll jsut have to fix it quickly if I screwed them up...

@mpeyper mpeyper merged commit a31f0fc into master Mar 31, 2019
@mpeyper mpeyper deleted the error-updates branch June 8, 2020 13:11
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.

Testing custom hook throwing error for incorrect arguments
4 participants