diff --git a/src/lib/find-vue-components.js b/src/lib/find-vue-components.js index b43ffc670..e293a7b8f 100644 --- a/src/lib/find-vue-components.js +++ b/src/lib/find-vue-components.js @@ -8,7 +8,7 @@ import { throwError } from './util' -function findAllVueComponentsFromVm ( +export function findAllVueComponentsFromVm ( vm: Component, components: Array = [] ): Array { diff --git a/src/mount.js b/src/mount.js index 9f23ccb3f..3fc0d350c 100644 --- a/src/mount.js +++ b/src/mount.js @@ -8,6 +8,7 @@ import createElement from './lib/create-element' import './lib/polyfills/matches-polyfill' import './lib/polyfills/object-assign-polyfill' import errorHandler from './lib/error-handler' +import { findAllVueComponentsFromVm } from './lib/find-vue-components' Vue.config.productionTip = false Vue.config.errorHandler = errorHandler @@ -25,8 +26,10 @@ export default function mount (component: Component, options: Options = {}): Vue vm.$mount() } - if (vm._error) { - throw (vm._error) + const componentsWithError = findAllVueComponentsFromVm(vm).filter(c => c._error) + + if (componentsWithError.length > 0) { + throw (componentsWithError[0]._error) } return new VueWrapper(vm, { attachedToDocument: !!options.attachToDocument }) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 050627823..b4cbcb545 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -175,7 +175,27 @@ describe('mount', () => { } const fn = () => mount(TestComponent) - expect(fn).to.throw() + expect(fn).to.throw('Error in mounted') + }) + + it('propagates errors when they are thrown by a nested component', () => { + const childComponent = { + template: '
', + mounted: function () { + throw new Error('Error in mounted') + } + } + const rootComponent = { + render: function (h) { + return h('div', [h(childComponent)]) + } + } + + const fn = () => { + mount(rootComponent) + } + + expect(fn).to.throw('Error in mounted') }) it('overwrites the component options with the options other than the mounting options when the options for mount contain those', () => {