From 4d517022cf96de33e9b9c150e34f10548c92252a Mon Sep 17 00:00:00 2001 From: xiaowei-one Date: Tue, 28 Feb 2023 20:25:53 +0800 Subject: [PATCH] fix: trigger when adding new property with Vue.seta and piWatch test --- src/v3/apiWatch.ts | 13 +++++++++++-- test/unit/features/v3/apiWatch.spec.ts | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/v3/apiWatch.ts b/src/v3/apiWatch.ts index 141a58eb686..c6208691024 100644 --- a/src/v3/apiWatch.ts +++ b/src/v3/apiWatch.ts @@ -218,8 +218,17 @@ function doWatch( }) } else if (isFunction(source)) { if (cb) { - // getter with cb - getter = () => call(source, WATCHER_GETTER) + const ob = call(source, WATCHER_GETTER) + if (isReactive(ob)){ + getter = () => { + ;(ob as any).__ob__.dep.depend() + return ob + } + deep = true + } else { + // getter with cb + getter = () => ob + } } else { // no cb -> simple effect getter = () => { diff --git a/test/unit/features/v3/apiWatch.spec.ts b/test/unit/features/v3/apiWatch.spec.ts index c92bc150ae7..e34694a0baa 100644 --- a/test/unit/features/v3/apiWatch.spec.ts +++ b/test/unit/features/v3/apiWatch.spec.ts @@ -1200,4 +1200,15 @@ describe('api: watch', () => { expect(parentSpy).toHaveBeenCalledTimes(1) expect(childSpy).toHaveBeenCalledTimes(1) }) + + // #12967 + test('trigger when adding new property with Vue.set', async () => { + const spy = vi.fn() + const r = reactive({ exist: 5 }) + watch(() => r, spy, { deep: true }) + set(r, 'add', 1) + + await nextTick() + expect(spy).toHaveBeenCalledTimes(1) + }) })