Skip to content

Commit

Permalink
fix(mount): throw the first error encountered during mount (#2428)
Browse files Browse the repository at this point in the history
Fixes #2319
  • Loading branch information
taku-y-9308 authored May 2, 2024
1 parent f3bbb6b commit 9332127
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ export function mount(
// Workaround for https://github.com/vuejs/core/issues/7020
const originalErrorHandler = app.config.errorHandler

let errorOnMount = null
let errorsOnMount: unknown[] = []
app.config.errorHandler = (err, instance, info) => {
errorOnMount = err
errorsOnMount.push(err)

return originalErrorHandler?.(err, instance, info)
}
Expand All @@ -100,9 +100,9 @@ export function mount(
to.appendChild(el)
}
const vm = app.mount(el)

if (errorOnMount) {
throw errorOnMount
if (errorsOnMount.length) {
// If several errors are thrown during mount, then throw the first one
throw errorsOnMount[0]
}
app.config.errorHandler = originalErrorHandler

Expand Down
11 changes: 11 additions & 0 deletions tests/mount.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ describe('mount: general tests', () => {
expect(wrapper.html()).toBe('<div>hello</div>')
})

it('should throw the first error encountered when mounting the component', () => {
const ThrowingComponent = defineComponent({
setup() {
throw new Error('Boom!')
},
template: '<div>{{ x.y }}</div>'
})

expect(() => mount(ThrowingComponent)).toThrowError('Boom!')
})

it('should not warn on readonly hasOwnProperty when mounting a component', () => {
const spy = vi.spyOn(console, 'warn').mockImplementation(() => {})

Expand Down

0 comments on commit 9332127

Please sign in to comment.