Skip to content

SSR component testing #561

Closed
Closed
@ruhaise

Description

@ruhaise

Describe the feature you'd like:

Currently there is no documentation, recommendations or API's that I could find regarding testing components that are rendered in a server side context.

Suggested implementation:

Describe alternatives you've considered:

currently, I use the hack

/**
 * @jest-environment node
 */
import React from 'react'
import { renderToString } from 'react-dom/server'

const foo = () => <img src="bar.png"/>
describe('Server-side rendering', () => {
  it('renders on a server without crashing', () => {
    const foo = () => <img src="bar.png"/> 
    const renderOnServer = () =>
      renderToString(
        <main>
              <foo/>
        </main>,
      )
    expect(renderOnServer).not.toThrow()
  })

  it('renders with the src attribute', () => {
    const foo = () => <img src="bar.png"/>
    const ElementImageHtml = renderToString(<foo/>)
    expect(ElementImageHtml).toContain('src="bar.png"')
  })
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs discussionWhether changes are needed is still undecided and additional discussion is needed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions