Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 2d85441

Browse files
authored
test(runtime-core): test effectscope instance null (#8397)
1 parent c0b9bd1 commit 2d85441

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
defineComponent,
66
getCurrentInstance,
77
nextTick,
8+
onErrorCaptured,
89
reactive,
910
ref,
1011
watch,
@@ -1576,4 +1577,60 @@ describe('api: watch', () => {
15761577
expect(spy).toHaveBeenCalledTimes(1)
15771578
expect(foo.value.a).toBe(2)
15781579
})
1580+
1581+
test('watch immediate error in effect scope should be catched by onErrorCaptured', async () => {
1582+
const warn = vi.spyOn(console, 'warn')
1583+
warn.mockImplementation(() => {})
1584+
const ERROR_IN_SCOPE = 'ERROR_IN_SCOPE'
1585+
const ERROR_OUT_SCOPE = 'ERROR_OUT_SCOPE'
1586+
1587+
const errors = ref<string[]>([])
1588+
const Comp = {
1589+
setup() {
1590+
const trigger = ref(0)
1591+
1592+
effectScope(true).run(() => {
1593+
watch(
1594+
trigger,
1595+
() => {
1596+
throw new Error(ERROR_IN_SCOPE)
1597+
},
1598+
{ immediate: true },
1599+
)
1600+
})
1601+
1602+
watchEffect(() => {
1603+
throw new Error(ERROR_OUT_SCOPE)
1604+
})
1605+
1606+
return () => ''
1607+
},
1608+
}
1609+
1610+
const root = nodeOps.createElement('div')
1611+
render(
1612+
h(
1613+
{
1614+
setup(_, { slots }) {
1615+
onErrorCaptured(e => {
1616+
errors.value.push(e.message)
1617+
return false
1618+
})
1619+
1620+
return () => h('div', slots.default && slots.default())
1621+
},
1622+
},
1623+
null,
1624+
() => [h(Comp)],
1625+
),
1626+
root,
1627+
)
1628+
await nextTick()
1629+
// only watchEffect as ran so far
1630+
expect(errors.value).toHaveLength(2)
1631+
expect(errors.value[0]).toBe(ERROR_IN_SCOPE)
1632+
expect(errors.value[1]).toBe(ERROR_OUT_SCOPE)
1633+
1634+
warn.mockRestore()
1635+
})
15791636
})

0 commit comments

Comments
 (0)