Skip to content

Commit

Permalink
fix(mount): correctly work with component throwing on mount
Browse files Browse the repository at this point in the history
* workaround for vuejs/core#7020
  • Loading branch information
xanf authored and cexbrayat committed Nov 13, 2022
1 parent 76673a1 commit 74c9af4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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>')
})
})

0 comments on commit 74c9af4

Please sign in to comment.