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(mount): correctly work with component throwing on mount #1845

Merged
merged 1 commit into from Nov 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/mount.ts
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions 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: '<div>hello</div>'
})

expect(() =>
mount(ThrowingComponent, { props: { blowup: true } })
).toThrow()

const wrapper = mount(ThrowingComponent, { props: { blowup: false } })

expect(wrapper.html()).toBe('<div>hello</div>')
})
})