From 5dc593ba2833e98eab12bd6c73765805b172185a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20L=C3=BCnborg?= Date: Thu, 10 Nov 2022 00:19:35 +0100 Subject: [PATCH] fix(watch): for immediate watch with single source, ensure cb is called with undefined as oldValue (#7075) fix: #7074 --- packages/runtime-core/__tests__/apiWatch.spec.ts | 2 +- packages/runtime-core/src/apiWatch.ts | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 029146b5205..f6bad14ddb9 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -745,7 +745,7 @@ describe('api: watch', () => { const state = ref() const spy = jest.fn() watch(() => state.value, spy, { immediate: true }) - expect(spy).toHaveBeenCalled() + expect(spy).toHaveBeenCalledWith(undefined, undefined, expect.any(Function)) state.value = 3 await nextTick() expect(spy).toHaveBeenCalledTimes(2) diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 99445be77d2..4999e11f0c9 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -333,10 +333,11 @@ function doWatch( callWithAsyncErrorHandling(cb, instance, ErrorCodes.WATCH_CALLBACK, [ newValue, // pass undefined as the old value when it's changed for the first time - oldValue === INITIAL_WATCHER_VALUE || - (isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE) - ? [] - : oldValue, + oldValue === INITIAL_WATCHER_VALUE + ? undefined + : (isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE) + ? [] + : oldValue, onCleanup ]) oldValue = newValue