test(ssr): add SSR and hydration tests for 'getServerSnapshot'#1209
test(ssr): add SSR and hydration tests for 'getServerSnapshot'#1209sukvvon wants to merge 4 commits intopmndrs:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
commit: |
dai-shi
left a comment
There was a problem hiding this comment.
Unless I don't misunderstand, this is not a valid test.
tests/basic.test.tsx
Outdated
| ) | ||
|
|
||
| expect(view).toContain('count:') | ||
| expect(view).toContain('0') |
There was a problem hiding this comment.
I think this test still passes even if we remove getServerSnapshot.
There was a problem hiding this comment.
@dai-shi I verified by removing getServerSnapshot — the test fails with Missing getServerSnapshot, which is required for server-rendered content. So renderToString does invoke getServerSnapshot, and this test validates its presence.
There was a problem hiding this comment.
Okay, but this test doesn't cover the behavior of getServerSnapshot. For example, if we have the getServerSnapshot implementation identical to that of getSnapshot, the test may pass.
There was a problem hiding this comment.
@dai-shi You're right — this test doesn't distinguish the behavior of getServerSnapshot from getSnapshot. However, it serves as a regression test to ensure getServerSnapshot is provided, since renderToString throws if it's missing. I also added a test (3aea407) that verifies the latest snapshot is returned after proxy state changes.
…fter state changes
dai-shi
left a comment
There was a problem hiding this comment.
I think I've got a better explanation.
The purpose of getServerSnapshot is to avoid hydration error.
So, the test should cover that. Testing if getServerSnapshot is defined is too trivial (only to increase the test coverage without much benefit.)
We've never tested if the current implementation can avoid hydration error.
Line 161 in 383ccf1
If the test reveals it, we need to fix the implementation.
| expect(view).toContain('5') | ||
| }) | ||
|
|
||
| it('should not cause hydration mismatch', async () => { |
There was a problem hiding this comment.
This is too naive. The AI doesn't understand the underlying problem.
Try to create a bug so that such a hydration-mismatch test fails.
I'm not sure if it's possible. But, otherwise, it's not valid for getServerSnapshot test.
Summary
ssr.test.tsxwith tests forgetServerSnapshotcallback inuseSnapshotgetServerSnapshotprevents hydration errorsrc/react.tsCheck List
pnpm run fixfor formatting and linting code and docs