From: Thorsten Lünborg Date: Wed, 9 Nov 2022 23:19:35 +0000 (+0100) Subject: fix(watch): for immediate watch with single source, ensure cb is called with undefine... X-Git-Tag: v3.2.44~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5dc593ba2833e98eab12bd6c73765805b172185a;p=thirdparty%2Fvuejs%2Fcore.git fix(watch): for immediate watch with single source, ensure cb is called with undefined as oldValue (#7075) fix: #7074 --- diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 029146b520..f6bad14ddb 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 99445be77d..4999e11f0c 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