Skip to content

Commit

Permalink
fix(server): remove act around server renderer to fix support for old…
Browse files Browse the repository at this point in the history
…er versions of react

* refactor(server/pure): remove unnecessary type annotation

* feat: add ssr.test.ts

This adds a new test to verify that renderHook can be called in an
SSR-like environment based on the changes implemented in #607.

* chore: update contributors table

* refactor: remove act call in render in sever/pure

@mpeyper explained how this `act` call in server rendering is not really
necessary so we can remove it.
  • Loading branch information
jsjoeio committed Jun 18, 2022
1 parent efd262c commit e2461ca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .all-contributorsrc
Expand Up @@ -145,7 +145,8 @@
"avatar_url": "https://avatars3.githubusercontent.com/u/3806031?v=4",
"profile": "https://jsjoe.io",
"contributions": [
"tutorial"
"tutorial",
"test"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -198,7 +198,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center"><a href="https://github.com/VinceMalone"><img src="https://avatars0.githubusercontent.com/u/2516349?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vince Malone</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=VinceMalone" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/doppelmutzi"><img src="https://avatars1.githubusercontent.com/u/4130968?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sebastian Weber</b></sub></a><br /><a href="#blog-doppelmutzi" title="Blogposts">📝</a></td>
<td align="center"><a href="https://gillchristian.xyz"><img src="https://avatars2.githubusercontent.com/u/8309423?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Christian Gill</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=gillchristian" title="Documentation">📖</a></td>
<td align="center"><a href="https://jsjoe.io"><img src="https://avatars3.githubusercontent.com/u/3806031?v=4?s=100" width="100px;" alt=""/><br /><sub><b>JavaScript Joe</b></sub></a><br /><a href="#tutorial-jsjoeio" title="Tutorials">✅</a></td>
<td align="center"><a href="https://jsjoe.io"><img src="https://avatars3.githubusercontent.com/u/3806031?v=4?s=100" width="100px;" alt=""/><br /><sub><b>JavaScript Joe</b></sub></a><br /><a href="#tutorial-jsjoeio" title="Tutorials">✅</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=jsjoeio" title="Tests">⚠️</a></td>
</tr>
<tr>
<td align="center"><a href="http://frontstuff.io"><img src="https://avatars1.githubusercontent.com/u/5370675?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sarah Dayan</b></sub></a><br /><a href="#platform-sarahdayan" title="Packaging/porting to new platform">📦</a></td>
Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/ssr.test.ts
@@ -0,0 +1,18 @@
/**
* @jest-environment node
*/
import { useState } from 'react'

// This verifies that renderHook can be called in
// a SSR-like environment.
describe('renderHook', () => {
function useLoading() {
const [loading, setLoading] = useState(false)
return { loading, setLoading }
}
runForRenderers(['server'], ({ renderHook }) => {
test('should not throw in SSR environment', () => {
expect(() => renderHook(() => useLoading())).not.toThrowError('document is not defined')
})
})
})
14 changes: 6 additions & 8 deletions src/server/pure.ts
Expand Up @@ -13,19 +13,17 @@ function createServerRenderer<TProps, TResult>(
) {
let renderProps: TProps | undefined
let container: HTMLDivElement | undefined
let serverOutput: string = ''
let serverOutput = ''
const testHarness = createTestHarness(rendererProps, wrapper, false)

return {
render(props?: TProps) {
renderProps = props
act(() => {
try {
serverOutput = ReactDOMServer.renderToString(testHarness(props))
} catch (e: unknown) {
rendererProps.setError(e as Error)
}
})
try {
serverOutput = ReactDOMServer.renderToString(testHarness(props))
} catch (e: unknown) {
rendererProps.setError(e as Error)
}
},
hydrate() {
if (container) {
Expand Down

0 comments on commit e2461ca

Please sign in to comment.