diff --git a/src/mount.ts b/src/mount.ts index 58d10d0e6..0bfd45f24 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -574,9 +574,24 @@ export function mount( } } + // Workaround for https://github.com/vuejs/core/issues/7020 + const originalErrorHandler = app.config.errorHandler + + let errorOnMount = null + app.config.errorHandler = (err, instance, info) => { + errorOnMount = err + + return originalErrorHandler?.(err, instance, info) + } + // mount the app! const vm = app.mount(el) + if (errorOnMount) { + throw errorOnMount + } + app.config.errorHandler = originalErrorHandler + const appRef = componentRef.value! as ComponentPublicInstance // we add `hasOwnProperty` so Jest can spy on the proxied vm without throwing // note that this is not necessary with Jest v27+ or Vitest, but is kept for compatibility with older Jest versions diff --git a/tests/mount.spec.ts b/tests/mount.spec.ts new file mode 100644 index 000000000..74f5b1c10 --- /dev/null +++ b/tests/mount.spec.ts @@ -0,0 +1,26 @@ +import { describe, expect, it } from 'vitest' +import { defineComponent } from 'vue' +import { mount } from '../src' + +describe('mount: general tests', () => { + it('correctly handles component, throwing on mount', () => { + // See https://github.com/vuejs/core/issues/7020 + const ThrowingComponent = defineComponent({ + props: ['blowup'], + mounted() { + if (this.blowup) { + throw new Error('Boom!') + } + }, + template: '
hello
' + }) + + expect(() => + mount(ThrowingComponent, { props: { blowup: true } }) + ).toThrow() + + const wrapper = mount(ThrowingComponent, { props: { blowup: false } }) + + expect(wrapper.html()).toBe('
hello
') + }) +})