Skip to content

Commit 74c9af4

Browse files
xanfcexbrayat
authored andcommitted
fix(mount): correctly work with component throwing on mount
* workaround for vuejs/core#7020
1 parent 76673a1 commit 74c9af4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/mount.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,24 @@ export function mount(
574574
}
575575
}
576576

577+
// Workaround for https://github.com/vuejs/core/issues/7020
578+
const originalErrorHandler = app.config.errorHandler
579+
580+
let errorOnMount = null
581+
app.config.errorHandler = (err, instance, info) => {
582+
errorOnMount = err
583+
584+
return originalErrorHandler?.(err, instance, info)
585+
}
586+
577587
// mount the app!
578588
const vm = app.mount(el)
579589

590+
if (errorOnMount) {
591+
throw errorOnMount
592+
}
593+
app.config.errorHandler = originalErrorHandler
594+
580595
const appRef = componentRef.value! as ComponentPublicInstance
581596
// we add `hasOwnProperty` so Jest can spy on the proxied vm without throwing
582597
// note that this is not necessary with Jest v27+ or Vitest, but is kept for compatibility with older Jest versions

tests/mount.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { defineComponent } from 'vue'
3+
import { mount } from '../src'
4+
5+
describe('mount: general tests', () => {
6+
it('correctly handles component, throwing on mount', () => {
7+
// See https://github.com/vuejs/core/issues/7020
8+
const ThrowingComponent = defineComponent({
9+
props: ['blowup'],
10+
mounted() {
11+
if (this.blowup) {
12+
throw new Error('Boom!')
13+
}
14+
},
15+
template: '<div>hello</div>'
16+
})
17+
18+
expect(() =>
19+
mount(ThrowingComponent, { props: { blowup: true } })
20+
).toThrow()
21+
22+
const wrapper = mount(ThrowingComponent, { props: { blowup: false } })
23+
24+
expect(wrapper.html()).toBe('<div>hello</div>')
25+
})
26+
})

0 commit comments

Comments
 (0)