Skip to content
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

feat: add renderToString function #1971

Merged
merged 1 commit into from Feb 19, 2023
Merged

Conversation

cexbrayat
Copy link
Member

It is now possible to use renderToString to test the SSR rendering of a component.

To do so, use renderToString(MyComponent).
renderToString returns a Promise<string> with the HTML rendered.

  it('returns correct html with pre-fetched data on server', async () => {
    const Component = defineComponent({
      template: '<div>{{ text }}</div>',
      setup() {
        const text = ref<string | null>(null)
        onServerPrefetch(async () => {
          text.value = await fakeFetch('onServerPrefetch')
        })
        return { text }
      }
    })

    const contents = await renderToString(Component)

    expect(contents).toBe('<div>onServerPrefetch</div>')
  })

@netlify
Copy link

netlify bot commented Feb 10, 2023

Deploy Preview for vue-test-utils-docs ready!

Name Link
🔨 Latest commit 71bdb19
🔍 Latest deploy log https://app.netlify.com/sites/vue-test-utils-docs/deploys/63e65010a7fa4c0008c6a761
😎 Deploy Preview https://deploy-preview-1971--vue-test-utils-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@cexbrayat
Copy link
Member Author

@lmiller1990 @freazlike This is a rebased/tidied up version of #1572
(@wobsoriano is still marked as the author of the commit 😉)

@cexbrayat
Copy link
Member Author

I forgot to mention I added types tests and a bit of documentation

It is now possible to use `renderToString` to test the SSR rendering of a component.

To do so, use `renderToString(MyComponent)`.
`renderToString` returns a `Promise<string>` with the HTML rendered.

```ts
  it('returns correct html with pre-fetched data on server', async () => {
    const Component = defineComponent({
      template: '<div>{{ text }}</div>',
      setup() {
        const text = ref<string | null>(null)
        onServerPrefetch(async () => {
          text.value = await fakeFetch('onServerPrefetch')
        })
        return { text }
      }
    })

    const contents = await renderToString(Component)

    expect(contents).toBe('<div>onServerPrefetch</div>')
  })
```
Copy link
Collaborator

@freakzlike freakzlike left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and I think it's a good starting point. 👍

Copy link
Member

@lmiller1990 lmiller1990 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's ship it and start to get feedback! Thanks @cexbrayat and @wobsoriano

src/renderToString.ts Show resolved Hide resolved
@lmiller1990 lmiller1990 merged commit 2aea499 into vuejs:main Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants