-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add renderToString function (#1971)
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>') }) ``` Co-authored-by: Robert Soriano <[email protected]>
- Loading branch information
1 parent
5761413
commit 2aea499
Showing
11 changed files
with
815 additions
and
370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Testing Server-side Rendering | ||
|
||
Vue Test Utils provides `renderToString` to test Vue applications that use server-side rendering (SSR). | ||
This guide will walk you through the process of testing a Vue application that uses SSR. | ||
|
||
## `renderToString` | ||
|
||
`renderToString` is a function that renders a Vue component to a string. | ||
It is an asynchronous function that returns a Promise, | ||
and accepts the same parameters as `mount` or `shallowMount`. | ||
|
||
Let's consider a simple component that uses the `onServerPrefetch` hook: | ||
|
||
```ts | ||
function fakeFetch(text: string) { | ||
return Promise.resolve(text) | ||
} | ||
|
||
const Component = defineComponent({ | ||
template: '<div>{{ text }}</div>', | ||
setup() { | ||
const text = ref<string | null>(null) | ||
|
||
onServerPrefetch(async () => { | ||
text.value = await fakeFetch('onServerPrefetch') | ||
}) | ||
|
||
return { text } | ||
} | ||
}) | ||
``` | ||
|
||
You can write a test for this component using `renderToString`: | ||
|
||
```ts | ||
import { renderToString } from '@vue/test-utils' | ||
|
||
it('renders the value returned by onServerPrefetch', async () => { | ||
const contents = await renderToString(Component) | ||
expect(contents).toBe('<div>onServerPrefetch</div>') | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.