-
Notifications
You must be signed in to change notification settings - Fork 233
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
Better error handling for hydrate in non-browser environment #796
Comments
As you suspected, the As such, I don’t see option 1 so much as a Band-Aid as ensuring the test environment matches the scenario you are wanting to test. That said, I think there are improvements we can make to the As for option 3, I’m not opposed to the idea of being able to pass in a document to render into, which could be defaulted to the global document for backwards compatibility. I would think it needs to be supported in the DOM renderer as well for consistency though. Speaking of consistency, another option might be to follow what |
Ah, happy to hear you say that. I think I confused myself around the original PR and what it was fixing. I think I can add a test that ensures
That's a great point! Should I open a separate issue for that, add details and then you can mark as a "good first issue"? (I can comment on it saying I'm happy to mentor whoever picks it up). |
I don’t mind if you want to use this issue for it, or raise a new one if that makes it more consumable to new comers, whichever you prefer. |
Guess that's easier. I'll update the title and add some notes here. |
Problem
SolutionCheck for Additional NotesI know how to solve this but I'd rather let someone else do it and offer mentorship in return. If someone else wants to take this on and wants guidance, ping me.
This is a great resource: Assertion Functions in TypeScript |
@mpeyper can I pick this up? If so, the user friendly error message can be |
react-hooks-testing-library
version: v8.0.0-alpha.1 - created a commit starting herereact
version: N/Areact-dom
version (if applicable): N/Areact-test-renderer
version (if applicable): N/Anode
version: v14.17.6npm
(oryarn
) version: v1.22.17Relevant code or config:
See below
What you did:
I added a test that runs in a Node environment to see if #605 was fixed (sidenote: using this issue/solution in a TypeScript course I'm authoring).
What happened:
I thought the solution would allow
renderHook
to be called in SSR environments but we get the same issue.Note: I added the original author on Discord to try and contact them about this PR. I saw in the comments they said they tested in a Node environment so I may be doing something differently.
Reproduction:
git clone https://github.com/testing-library/react-hooks-testing-library.git
touch src/server/__tests__/ssr.test.ts
yarn test --watch=false
Problem description:
Even though #607 modified
src/server/pure.ts
to fill the container with the ReactDOM tree inhydrate()
, this still assumesdocument
is always available.Suggested solution:
I see three potential solutions.
Option 1: add DOM to test before
hydrate
Based on my understanding,
hydrate
is supposed to be called on the client. So in our test, we could create a mockdocument
and make sure it's available globally before callinghydrate
. I see this more like a bandaid though.Option 2: check if
document
is definedAdd a check to see if
document
is available before using it.Option 3: pass in
document
This is the approach I usually take, but it would be a breaking change and most people don't like passing in globals this way.
The text was updated successfully, but these errors were encountered: