You’re browsing the documentation for Vue Test Utils for Vue v2.x and earlier.
To read docs for Vue Test Utils for Vue 3, click here.
O método renderToString()
Argumentos:
{Component} component
{Object} options
{Object} context
{Array<Component|Object>|Component} children
{Object} slots
{Array<Component|Object>|Component|String} default
{Array<Component|Object>|Component|String} named
{Object} mocks
{Object|Array<string>} stubs
{Vue} localVue
Retorna:
{Promise<string>}
Opções:
Consulte as opções
- Uso:
Transforma um componente em HTML.
O renderToString
usa o vue-server-renderer
nos bastidores, para transformar um componente em HTML.
O renderToString
está incluído dentro do pacote @vue/server-test-utils
.
Sem as opções:
import { renderToString } from '@vue/server-test-utils'
import Foo from './Foo.vue'
describe('Foo', () => {
it('renders a div', async () => {
const str = await renderToString(Foo)
expect(str).toContain('<div></div>')
})
})
Com as opções do Vue:
import { renderToString } from '@vue/server-test-utils'
import Foo from './Foo.vue'
describe('Foo', () => {
it('renders a div', async () => {
const str = await renderToString(Foo, {
propsData: {
color: 'red'
}
})
expect(str).toContain('red')
})
})
Encaixes padrão e nomeados:
import { renderToString } from '@vue/server-test-utils'
import Foo from './Foo.vue'
import Bar from './Bar.vue'
import FooBar from './FooBar.vue'
describe('Foo', () => {
it('renders a div', async () => {
const str = await renderToString(Foo, {
slots: {
default: [Bar, FooBar],
fooBar: FooBar, // Corresponderá a <slot name="FooBar" />,
foo: '<div />'
}
})
expect(str).toContain('<div></div>')
})
})
Forjando propriedades globais:
import { renderToString } from '@vue/server-test-utils'
import Foo from './Foo.vue'
describe('Foo', () => {
it('renders a div', async () => {
const $route = { path: 'http://www.example-path.com' }
const str = await renderToString(Foo, {
mocks: {
$route
}
})
expect(str).toContain($route.path)
})
})