Skip to content

Commit e2461ca

Browse files
authoredJun 18, 2022
fix(server): remove act around server renderer to fix support for older 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.
1 parent efd262c commit e2461ca

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed
 

‎.all-contributorsrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@
145145
"avatar_url": "https://avatars3.githubusercontent.com/u/3806031?v=4",
146146
"profile": "https://jsjoe.io",
147147
"contributions": [
148-
"tutorial"
148+
"tutorial",
149+
"test"
149150
]
150151
},
151152
{

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
198198
<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>
199199
<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>
200200
<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>
201-
<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>
201+
<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>
202202
</tr>
203203
<tr>
204204
<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>

‎src/__tests__/ssr.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
import { useState } from 'react'
5+
6+
// This verifies that renderHook can be called in
7+
// a SSR-like environment.
8+
describe('renderHook', () => {
9+
function useLoading() {
10+
const [loading, setLoading] = useState(false)
11+
return { loading, setLoading }
12+
}
13+
runForRenderers(['server'], ({ renderHook }) => {
14+
test('should not throw in SSR environment', () => {
15+
expect(() => renderHook(() => useLoading())).not.toThrowError('document is not defined')
16+
})
17+
})
18+
})

‎src/server/pure.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@ function createServerRenderer<TProps, TResult>(
1313
) {
1414
let renderProps: TProps | undefined
1515
let container: HTMLDivElement | undefined
16-
let serverOutput: string = ''
16+
let serverOutput = ''
1717
const testHarness = createTestHarness(rendererProps, wrapper, false)
1818

1919
return {
2020
render(props?: TProps) {
2121
renderProps = props
22-
act(() => {
23-
try {
24-
serverOutput = ReactDOMServer.renderToString(testHarness(props))
25-
} catch (e: unknown) {
26-
rendererProps.setError(e as Error)
27-
}
28-
})
22+
try {
23+
serverOutput = ReactDOMServer.renderToString(testHarness(props))
24+
} catch (e: unknown) {
25+
rendererProps.setError(e as Error)
26+
}
2927
},
3028
hydrate() {
3129
if (container) {

0 commit comments

Comments
 (0)
Please sign in to comment.