This repository was archived by the owner on Jul 19, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed
packages/runtime-core/__tests__ Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change 5
5
defineComponent ,
6
6
getCurrentInstance ,
7
7
nextTick ,
8
+ onErrorCaptured ,
8
9
reactive ,
9
10
ref ,
10
11
watch ,
@@ -1576,4 +1577,60 @@ describe('api: watch', () => {
1576
1577
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
1577
1578
expect ( foo . value . a ) . toBe ( 2 )
1578
1579
} )
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
+ } )
1579
1636
} )
You can’t perform that action at this time.
0 commit comments