diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 0c3719e7364..f01b1338b7e 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -859,7 +859,7 @@ describe('api: watch', () => { expect(instance!.effects![0].active).toBe(false) }) - test('this.$watch should pass `this.proxy` to watch source as the first argument ', () => { + test('this.$watch should pass `this.proxy` to watch source as the first argument', () => { let instance: any const source = jest.fn() @@ -878,6 +878,22 @@ describe('api: watch', () => { expect(source).toHaveBeenCalledWith(instance) }) + test('should not leak `this.proxy` to setup()', () => { + const source = jest.fn() + + const Comp = defineComponent({ + render() {}, + setup() { + watch(source, () => {}) + } + }) + + const root = nodeOps.createElement('div') + createApp(Comp).mount(root) + + expect(source).toBeCalledWith(undefined) + }) + // #2728 test('pre watcher callbacks should not track dependencies', async () => { const a = ref(0) diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 9498ced48fb..0eb9db55e68 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -181,9 +181,7 @@ function doWatch( } else if (isReactive(s)) { return traverse(s) } else if (isFunction(s)) { - return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER, [ - instance && (instance.proxy as any) - ]) + return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER) } else { __DEV__ && warnInvalidSource(s) } @@ -193,7 +191,7 @@ function doWatch( // getter with cb getter = () => callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER, [ - instance && (instance.proxy as any) + arguments[3] && arguments[3].proxy ]) } else { // no cb -> simple effect