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

Fix/better error handling for hydrate #862

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

amovar18
Copy link

What:
This PR adds a custom error message for hydrate function.

Why:
When hydrate function is called in non-browser environment it throws an error document is not defined which lead to a non-meaningful error message. This PR now aims to check if document is available or not and then throws a custom error message.

How:
This PR just provides a wrapper of an if else block inside the hydrate function and throws a custom error message saying Hydrate function can only be called in a client environment with a document available..

Checklist:

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

SideNote: The documentation states that to use hydrate function for SSR testing of a hook but the conversation in the issue stated that render should be used in non browser environment and hydrate should be used on environment where document is available. Should the documentation be updated?

@netlify
Copy link

netlify bot commented Jun 23, 2022

Deploy Preview for react-hooks-testing-library failed.

Name Link
🔨 Latest commit ce72ef4
🔍 Latest deploy log https://app.netlify.com/sites/react-hooks-testing-library/deploys/62b3f4d1a507e6000969c907

@mpeyper
Copy link
Member

mpeyper commented Jun 23, 2022

Thanks @amovar1. I do not have a heap of time to look at this, but I’ll try to get to it this weekend.

A couple of notes from my quick look now:

  1. Can we write a test for this? It should be possible to see the error by calling hydrate in this test file as there is no document available there (it’s meant to replicate a ssr environment).
  2. I think the docs are ok on this as in the case of the ssr renderer, render simulates the server behaviour, and hydrate simulates the client behaviour. That said, updates to add clarity in the docs are always welcome (just be aware that our docs generation is currently broken so might be wasted effort).
  3. Speaking of the docs, don’t worry about the netlify errors. This was already broken and unrelated to your changes.

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.

2 participants