Browse Source

tests for ssr bundle renderer

dev
Evan You 9 years ago
parent
commit
b791c5722f
  1. 4
      src/server/create-bundle-renderer.js
  2. 29
      src/server/run-in-vm.js
  3. 12
      test/ssr/fixtures/app.js
  4. 1
      test/ssr/fixtures/error.js
  5. 3
      test/ssr/jasmine.json
  6. 80
      test/ssr/ssr-bundle-render.spec.js

4
src/server/create-bundle-renderer.js

@ -6,6 +6,10 @@ export function createBundleRendererCreator (createRenderer) {
const renderer = createRenderer(rendererOptions)
return {
renderToString: (context, cb) => {
if (typeof context === 'function') {
cb = context
context = {}
}
runInVm(code, context).then(app => {
renderer.renderToString(app, cb)
}).catch(cb)

29
src/server/run-in-vm.js

@ -12,24 +12,25 @@ function createContext (context) {
setTimeout,
console,
process,
__VUE_SSR_CONTEXT__: context || {}
__VUE_SSR_CONTEXT__: context
}
sandbox.global = sandbox
return sandbox
}
export default function runInVm (code, _context) {
const wrapper = NativeModule.wrap(code)
const context = createContext(_context)
const compiledWrapper = vm.runInNewContext(wrapper, context, {
filename: '__vue_ssr_bundle__',
displayErrors: true
export default function runInVm (code, _context = {}) {
return new Promise((resolve, reject) => {
const wrapper = NativeModule.wrap(code)
const context = createContext(_context)
const compiledWrapper = vm.runInNewContext(wrapper, context, {
filename: '__vue_ssr_bundle__',
displayErrors: true
})
const m = { exports: {}}
compiledWrapper.call(m.exports, m.exports, require, m)
const res = Object.prototype.hasOwnProperty.call(m.exports, 'default')
? m.exports.default
: m
resolve(typeof res === 'function' ? res(_context) : res)
})
const m = { exports: {}}
compiledWrapper.call(m.exports, m.exports, require, m)
const res = Object.prototype.hasOwnProperty.call(m.exports, 'default')
? m.exports.default
: m
// ensure returning a Promise
return Promise.resolve(res)
}

12
test/ssr/fixtures/app.js

@ -0,0 +1,12 @@
import Vue from '../../../dist/vue.common.js'
export default context => {
return new Promise(resolve => {
context.msg = 'hello'
resolve(new Vue({
render (h) {
return h('div', context.url)
}
}))
})
}

1
test/ssr/fixtures/error.js

@ -0,0 +1 @@
throw new Error('foo')

3
test/ssr/jasmine.json

@ -3,7 +3,8 @@
"spec_files": [
"ssr-env.spec.js",
"ssr-string.spec.js",
"ssr-stream.spec.js"
"ssr-stream.spec.js",
"ssr-bundle-render.spec.js"
],
"helpers": [
"../../node_modules/babel-register/lib/node.js"

80
test/ssr/ssr-bundle-render.spec.js

@ -0,0 +1,80 @@
import path from 'path'
import webpack from 'webpack'
import MemoeryFS from 'memory-fs'
import { createBundleRenderer } from '../../packages/vue-server-renderer'
const rendererCache = {}
function createRenderer (file, cb) {
if (rendererCache[file]) {
return cb(rendererCache[file])
}
const compiler = webpack({
target: 'node',
entry: path.resolve(__dirname, 'fixtures', file),
output: {
path: '/',
filename: 'bundle.js',
libraryTarget: 'commonjs2'
},
module: {
loaders: [{ test: /\.js$/, loader: 'babel' }]
}
})
const fs = new MemoeryFS()
compiler.outputFileSystem = fs
compiler.run((err, stats) => {
expect(err).toBeFalsy()
expect(stats.errors).toBeFalsy()
const code = fs.readFileSync('/bundle.js', 'utf-8')
const renderer = rendererCache[file] = createBundleRenderer(code)
cb(renderer)
})
}
describe('SSR: bundle renderer', () => {
it('renderToString', done => {
createRenderer('app.js', renderer => {
const context = { url: '/test' }
renderer.renderToString(context, (err, res) => {
expect(res).toBe('<div server-rendered="true">&sol;test</div>')
expect(context.msg).toBe('hello')
done()
})
})
})
it('renderToStream', done => {
createRenderer('app.js', renderer => {
const context = { url: '/test' }
const stream = renderer.renderToStream(context)
let res = ''
stream.on('data', chunk => {
res += chunk.toString()
})
stream.on('end', () => {
expect(res).toBe('<div server-rendered="true">&sol;test</div>')
expect(context.msg).toBe('hello')
done()
})
})
})
it('renderToString catch error', done => {
createRenderer('error.js', renderer => {
renderer.renderToString(err => {
expect(err.message).toBe('foo')
done()
})
})
})
it('renderToStream catch error', done => {
createRenderer('error.js', renderer => {
const stream = renderer.renderToStream()
stream.on('error', err => {
expect(err.message).toBe('foo')
done()
})
})
})
})
Loading…
Cancel
Save