diff --git a/src/core/instance/render.js b/src/core/instance/render.js index b009a491..902b9624 100644 --- a/src/core/instance/render.js +++ b/src/core/instance/render.js @@ -63,6 +63,12 @@ export function renderMixin (Vue: Class) { let vnode = render.call(vm._renderProxy) // return empty vnode in case the render function errored out if (!(vnode instanceof VNode)) { + if (process.env.NODE_ENV !== 'production' && Array.isArray(vnode)) { + warn( + 'Multiple root nodes returned from render function. Render function ' + + 'should return a single root node.' + ) + } vnode = emptyVNode } // set parent diff --git a/test/unit/features/component/component-slot.spec.js b/test/unit/features/component/component-slot.spec.js index 85d516fe..c0b50bc9 100644 --- a/test/unit/features/component/component-slot.spec.js +++ b/test/unit/features/component/component-slot.spec.js @@ -268,4 +268,48 @@ describe('Component slot', () => { ) Vue.config.preserveWhitespace = false }) + + it('programmatic access to $slots', () => { + const vm = new Vue({ + template: '

A

C

B

', + components: { + test: { + render () { + expect(this.$slots.a.length).toBe(1) + expect(this.$slots.a[0].tag).toBe('p') + expect(this.$slots.a[0].children.length).toBe(1) + expect(this.$slots.a[0].children[0].text).toBe('A') + + expect(this.$slots.b.length).toBe(1) + expect(this.$slots.b[0].tag).toBe('p') + expect(this.$slots.b[0].children.length).toBe(1) + expect(this.$slots.b[0].children[0].text).toBe('B') + + expect(this.$slots.default.length).toBe(1) + expect(this.$slots.default[0].tag).toBe('div') + expect(this.$slots.default[0].children.length).toBe(1) + expect(this.$slots.default[0].children[0].text).toBe('C') + + return this.$slots.default[0] + } + } + } + }).$mount() + expect(vm.$el.tagName).toBe('DIV') + expect(vm.$el.textContent).toBe('C') + }) + + it('warn if user directly returns array', () => { + new Vue({ + template: '
', + components: { + test: { + render () { + return this.$slots.default + } + } + } + }).$mount() + expect('Render function should return a single root node').toHaveBeenWarned() + }) })