Skip to content

Commit 7b3867e

Browse files
authored
fix(ssr): Add ability to run SSR tests in pure-node environment (#607)
* fix(ssr): div container created only during hydration It allows to run tests in real node environment, where no DOM global objects exists. Fix: #605 * chore: add xobotyi to hall of fame %)
1 parent 0543a9e commit 7b3867e

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

.all-contributorsrc

+10
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,16 @@
519519
"contributions": [
520520
"maintenance"
521521
]
522+
},
523+
{
524+
"login": "xobotyi",
525+
"name": "Anton Zinovyev",
526+
"avatar_url": "https://avatars.githubusercontent.com/u/6178739?v=4",
527+
"profile": "https://github.com/xobotyi",
528+
"contributions": [
529+
"bug",
530+
"code"
531+
]
522532
}
523533
],
524534
"skipCi": true,

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
239239
<td align="center"><a href="https://aleksandar.xyz"><img src="https://avatars.githubusercontent.com/u/7226555?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aleksandar Grbic</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=agjs" title="Documentation">📖</a></td>
240240
<td align="center"><a href="https://github.com/yoniholmes"><img src="https://avatars.githubusercontent.com/u/184589?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jonathan Holmes</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=yoniholmes" title="Code">💻</a></td>
241241
<td align="center"><a href="https://michaeldeboey.be"><img src="https://avatars.githubusercontent.com/u/6643991?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michaël De Boey</b></sub></a><br /><a href="#maintenance-MichaelDeBoey" title="Maintenance">🚧</a></td>
242+
<td align="center"><a href="https://github.com/xobotyi"><img src="https://avatars.githubusercontent.com/u/6178739?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Anton Zinovyev</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/issues?q=author%3Axobotyi" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=xobotyi" title="Code">💻</a></td>
242243
</tr>
243244
</table>
244245

src/server/pure.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import ReactDOMServer from 'react-dom/server'
22
import ReactDOM from 'react-dom'
33
import { act } from 'react-dom/test-utils'
44

5-
import { RendererProps, RendererOptions } from '../types/react'
5+
import { RendererOptions, RendererProps } from '../types/react'
66

77
import { createRenderHook } from '../core'
88
import { createTestHarness } from '../helpers/createTestHarness'
@@ -12,44 +12,44 @@ function createServerRenderer<TProps, TResult>(
1212
{ wrapper }: RendererOptions<TProps>
1313
) {
1414
let renderProps: TProps | undefined
15-
let hydrated = false
16-
const container = document.createElement('div')
15+
let container: HTMLDivElement | undefined
16+
let serverOutput: string = ''
1717
const testHarness = createTestHarness(rendererProps, wrapper, false)
1818

1919
return {
2020
render(props?: TProps) {
2121
renderProps = props
2222
act(() => {
2323
try {
24-
const serverOutput = ReactDOMServer.renderToString(testHarness(props))
25-
container.innerHTML = serverOutput
24+
serverOutput = ReactDOMServer.renderToString(testHarness(props))
2625
} catch (e: unknown) {
2726
rendererProps.setError(e as Error)
2827
}
2928
})
3029
},
3130
hydrate() {
32-
if (hydrated) {
31+
if (container) {
3332
throw new Error('The component can only be hydrated once')
3433
} else {
34+
container = document.createElement('div')
35+
container.innerHTML = serverOutput
3536
act(() => {
36-
ReactDOM.hydrate(testHarness(renderProps), container)
37+
ReactDOM.hydrate(testHarness(renderProps), container!)
3738
})
38-
hydrated = true
3939
}
4040
},
4141
rerender(props?: TProps) {
42-
if (!hydrated) {
42+
if (!container) {
4343
throw new Error('You must hydrate the component before you can rerender')
4444
}
4545
act(() => {
46-
ReactDOM.render(testHarness(props), container)
46+
ReactDOM.render(testHarness(props), container!)
4747
})
4848
},
4949
unmount() {
50-
if (hydrated) {
50+
if (container) {
5151
act(() => {
52-
ReactDOM.unmountComponentAtNode(container)
52+
ReactDOM.unmountComponentAtNode(container!)
5353
})
5454
}
5555
},

0 commit comments

Comments
 (0)