Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/issue 2319 throw first error thrown during mount #2428

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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