-
Notifications
You must be signed in to change notification settings - Fork 232
feat(server): add SSR test for renderHook #799
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
Changes from all commits
5247d27
93d927d
59c752f
c37e8a4
9d35e1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,19 +13,17 @@ function createServerRenderer<TProps, TResult>( | |
) { | ||
let renderProps: TProps | undefined | ||
let container: HTMLDivElement | undefined | ||
let serverOutput: string = '' | ||
let serverOutput = '' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Since we give |
||
const testHarness = createTestHarness(rendererProps, wrapper, false) | ||
|
||
return { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. github won't let me comment on the actual line, but the error in the test is being caused by the You can replicate this by running: npm run install:react-16.9.0
npm test -- --no-watch src/__tests__/ssr.test.ts In the context of server rendering, the render(props?: TProps) {
renderProps = props
try {
serverOutput = ReactDOMServer.renderToString(testHarness(props))
} catch (e: unknown) {
rendererProps.setError(e as Error)
}
}, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, I don't think I would have figured that out on my own 😅 I'll test this out and make the necessary changes. Thanks a bunch! |
||
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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✍🏼 This technique is handy for running specific tests in a Node environment.
See docs.