From f0057b101e6451d5095cdb7fd6308fd31ac0450c Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 15 Aug 2022 19:06:38 +0800 Subject: [PATCH] fix(watch): avoid pre watcher firing on unmount fix #12703 --- src/v3/apiWatch.ts | 5 +---- test/unit/features/v3/apiWatch.spec.ts | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/v3/apiWatch.ts b/src/v3/apiWatch.ts index 7fe5eeb9dcc..141a58eb686 100644 --- a/src/v3/apiWatch.ts +++ b/src/v3/apiWatch.ts @@ -274,10 +274,7 @@ function doWatch( let oldValue = isMultiSource ? [] : INITIAL_WATCHER_VALUE // overwrite default run watcher.run = () => { - if ( - !watcher.active && - !(flush === 'pre' && instance && instance._isBeingDestroyed) - ) { + if (!watcher.active) { return } if (cb) { diff --git a/test/unit/features/v3/apiWatch.spec.ts b/test/unit/features/v3/apiWatch.spec.ts index 0206e8df3bf..c92bc150ae7 100644 --- a/test/unit/features/v3/apiWatch.spec.ts +++ b/test/unit/features/v3/apiWatch.spec.ts @@ -542,7 +542,7 @@ describe('api: watch', () => { expect(cb).not.toHaveBeenCalled() }) - it('should fire on component unmount w/ flush: pre', async () => { + it('should not fire on component unmount w/ flush: pre', async () => { const toggle = ref(true) const cb = vi.fn() const Comp = { @@ -560,7 +560,7 @@ describe('api: watch', () => { expect(cb).not.toHaveBeenCalled() toggle.value = false await nextTick() - expect(cb).toHaveBeenCalledTimes(1) + expect(cb).not.toHaveBeenCalled() }) // vuejs/core#1763