diff --git a/packages/reactivity/__tests__/watch.spec.ts b/packages/reactivity/__tests__/watch.spec.ts index 245acfd63..9bec54e5f 100644 --- a/packages/reactivity/__tests__/watch.spec.ts +++ b/packages/reactivity/__tests__/watch.spec.ts @@ -277,4 +277,16 @@ describe('watch', () => { expect(dummy).toEqual([1, 2, 3]) }) + + test('watch with immediate reset and sync flush', () => { + const value = ref(false) + + watch(value, () => { + value.value = false + }) + + value.value = true + value.value = true + expect(value.value).toBe(false) + }) }) diff --git a/packages/reactivity/src/watch.ts b/packages/reactivity/src/watch.ts index 659121ca3..648e6481b 100644 --- a/packages/reactivity/src/watch.ts +++ b/packages/reactivity/src/watch.ts @@ -264,11 +264,11 @@ export function watch( : oldValue, boundCleanup, ] + oldValue = newValue call ? call(cb!, WatchErrorCodes.WATCH_CALLBACK, args) : // @ts-expect-error cb!(...args) - oldValue = newValue } finally { activeWatcher = currentWatcher }