Skip to content

Commit 9332127

Browse files
authored
fix(mount): throw the first error encountered during mount (#2428)
Fixes #2319
1 parent f3bbb6b commit 9332127

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/mount.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ export function mount(
7474
// Workaround for https://github.com/vuejs/core/issues/7020
7575
const originalErrorHandler = app.config.errorHandler
7676

77-
let errorOnMount = null
77+
let errorsOnMount: unknown[] = []
7878
app.config.errorHandler = (err, instance, info) => {
79-
errorOnMount = err
79+
errorsOnMount.push(err)
8080

8181
return originalErrorHandler?.(err, instance, info)
8282
}
@@ -100,9 +100,9 @@ export function mount(
100100
to.appendChild(el)
101101
}
102102
const vm = app.mount(el)
103-
104-
if (errorOnMount) {
105-
throw errorOnMount
103+
if (errorsOnMount.length) {
104+
// If several errors are thrown during mount, then throw the first one
105+
throw errorsOnMount[0]
106106
}
107107
app.config.errorHandler = originalErrorHandler
108108

tests/mount.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ describe('mount: general tests', () => {
2828
expect(wrapper.html()).toBe('<div>hello</div>')
2929
})
3030

31+
it('should throw the first error encountered when mounting the component', () => {
32+
const ThrowingComponent = defineComponent({
33+
setup() {
34+
throw new Error('Boom!')
35+
},
36+
template: '<div>{{ x.y }}</div>'
37+
})
38+
39+
expect(() => mount(ThrowingComponent)).toThrowError('Boom!')
40+
})
41+
3142
it('should not warn on readonly hasOwnProperty when mounting a component', () => {
3243
const spy = vi.spyOn(console, 'warn').mockImplementation(() => {})
3344

0 commit comments

Comments
 (0)