From 3cd66869a7d55ac2b59c285971bc0a3a9d2fecdb Mon Sep 17 00:00:00 2001 From: cexbrayat Date: Tue, 26 Jan 2021 16:29:55 +0100 Subject: [PATCH 1/2] repro: spy on vm --- tests/vm.spec.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/vm.spec.ts b/tests/vm.spec.ts index 6f2a3b31d..136d74029 100644 --- a/tests/vm.spec.ts +++ b/tests/vm.spec.ts @@ -24,4 +24,25 @@ describe('vm', () => { expect(wrapper.vm.isEnabled).toBe(false) }) + + it('allows spying on vm', async () => { + const Component = defineComponent({ + name: 'VTUComponent', + template: '
{{ msg }}
', + setup() { + const msg = 'hello' + const isEnabled = ref(true) + const toggle = () => (isEnabled.value = !isEnabled.value) + return { msg, isEnabled, toggle } + } + }) + + const wrapper = mount(Component) + + jest.spyOn(wrapper.vm, 'toggle') + + await wrapper.get('div').trigger('click') + + expect(wrapper.vm.toggle).toHaveBeenCalled() + }) }) From d05502268d64c29befa532a82b63a942b5bcbbc6 Mon Sep 17 00:00:00 2001 From: cexbrayat Date: Tue, 26 Jan 2021 18:01:07 +0100 Subject: [PATCH 2/2] fix: add hasOwnProperty to wrapper.vm This allows Jest to spy on the proxied vm Fixes #309 --- src/mount.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mount.ts b/src/mount.ts index 13e7a756e..17ed85d20 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -441,8 +441,11 @@ export function mount( // if not, use the return value from app.mount. const appRef = vm.$refs[MOUNT_COMPONENT_REF] as ComponentPublicInstance const $vm = Reflect.ownKeys(appRef).length ? appRef : vm + // we add `hasOwnProperty` so jest can spy on the proxied vm without throwing + $vm.hasOwnProperty = (property) => { + return Reflect.has($vm, property) + } console.warn = warnSave - return createWrapper(app, $vm, setProps) }