infra for verifying server side rendering of components
See original GitHub issueWe need an infrastructure to ensure that all the components we build work with server side rendering out of the box. As of today, we write unit tests that use jsdom
so essentially it tests the browser environment only, on the other side we use storybook to render our components built on top of Create React App which again is client-side rendering only.
Issue Analytics
- State:
- Created 10 months ago
- Reactions:1
- Comments:9 (4 by maintainers)
Top Results From Across the Web
How to Enable Server-Side Rendering for a React App
In this tutorial, you will initialize a React app using Create React App and then modify the project to enable server-side rendering.
Read more >Server Side Rendering and its Relationship with SEO - Medium
Server -side rendering is the process of rendering a client-side JavaScript application to static HTML on the server. SSR is a large category ......
Read more >SSR component testing · Issue #561 · testing-library/react ...
Currently there is no documentation, recommendations or API's that I could find regarding testing components that are rendered in a server side ......
Read more >React Server Components - Patterns.dev
Today's Server-side rendering of client-side JavaScript can be suboptimal. JavaScript for your components is rendered on the server into an HTML string. This ......
Read more >Rendering: Server and Client Components - Next.js beta docs
Server Components (RFC) allow developers to better leverage server infrastructure. For example, large dependencies that previously would impact the ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
tried, tested and updated the section above 🚀
I started exploring this and there 2 approaches:
storybook
capable of doing SSROption 1 isn’t possible with the current architecture of
storybook
so that is out of the list.Option 2 has multiple angles to it: So basically the way react-testing-library along with jest works is that it uses
jsdom
under the hood which is basically an in-memory dom now since it’s an in-memory dom it mimics the browser. Because of this we really can’t test out if the components will fail when it useswindow
ordocument
instances since those objects will be available in thejsdom
environment. As of today basically we write tests as followsNow this by default will run with
jest-environment: jsdom
. To test server-side rendering we can do something like this:the problem with the above approach is that even though we call the
renderToString
function it still won’t throw for cases where the components have usedwindow
ordocument
because the jest environment is stilljsdom
andrenderToString
also works in browser environments so at this point of time jest doesn’t really understand that we intend to test therenderToString
in server(precisely node) environment. More details in this issueTo overcome the above we might try removing the global
window
anddocument
. The snippet for it will look something like below.This feels like a hack and won’t work because the dependent libraries are still operating in browser mode(because jest is working with
environment: jsdom
and there are libs likestyled-components
and a lot others that have libs for diff environment under the same package name and the bundler resolves thepackage.json
’smain
orbrowser
field based on the environment it runs in and over here even though we removewindow
anddocument
the actual environment will still be browser so these libs will start failing.So now what are the options at hand?
We can actually change the
jest-environment: node
to tell jest to run in node environment by doing followingnow, this will work and mimic the actual node server environment. the only downside is now we can only test that the components are rendered statically and prevent unwanted usages of
window
anddocument
instances in the server render phase. We can’t really usereact-testing-library
’srender
method and query dom things sincerender
from RTL is meant to run only in browser environments. There’s a comment regarding this from KentAlso, with the above approach now we need a separate test file for testing ssr since the
@jest-environment
pragma works at file level and not test levelProposal
I think for our use case what we can do is
@jest-environment node
and assert that our components render successfully innode
environment and prevent unwanted uses ofwindow
anddocument
.Edit
Another approach suggested by @saurabhdaware here would work better. it’ll gives us best of both worlds
@jest-environment node
and then after rendering the markup on server we can initialise jsdom and pass it the server-rendered markup to hydratehere’s how the test would look like