forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderToString.js
49 lines (42 loc) · 1.2 KB
/
renderToString.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// @flow
import Vue from 'vue'
import { createInstance, createRenderSlot } from 'create-instance'
import { throwError } from 'shared/util'
import { createRenderer } from 'vue-server-renderer'
import testUtils from '@vue/test-utils'
import { mergeOptions } from 'shared/merge-options'
import config from './config'
Vue.config.productionTip = false
Vue.config.devtools = false
export default function renderToString (
component: Component,
options: Options = {}
): string {
const renderer = createRenderer()
if (!renderer) {
throwError(
`renderToString must be run in node. It cannot be ` + `run in a browser`
)
}
// Remove cached constructor
delete component._Ctor
if (options.attachToDocument) {
throwError(`you cannot use attachToDocument with ` + `renderToString`)
}
const vueConstructor = testUtils.createLocalVue(options.localVue)
vueConstructor.prototype._t = createRenderSlot(options)
const vm = createInstance(
component,
mergeOptions(options, config),
vueConstructor
)
let renderedString = ''
// $FlowIgnore
renderer.renderToString(vm, (err, res) => {
if (err) {
console.log(err)
}
renderedString = res
})
return renderedString
}